SEO friendly URL PHP

Hello all

I have been trying to generate SEO friendly URL in my project.
I had succeeded in single pages Like about.php, contact.php becomes about, contact.

But where i send variables in the url, it doesn’t work out.
It means www.abc.com/admin/add-city.php?edt=3

I had list-city.php page where all cities are listed, if user wants to edit any one , it will click and user will be directed to add-city.php page for editing.

My code for list-city.php is

<?php
                    					    $sn = 1;
                    					    $city1 = mysqli_query($conn, "SELECT * FROM mCity WHERE is_delete = 'No' ORDER BY cityname ASC");
                    					    while($city2 = mysqli_fetch_array($city1))
                    					    {
                    					?>
                    						<tr>
                    							<td><?php echo $sn; ?></td>
                    							<td><?php echo ucwords($city2['cityName']); ?></td>
                    							<td>
                    								<!--<a href="add-city.php?edt=<?php echo $city2['cityid']; ?>" class="action-icon" title="Edit"> <i class="mdi mdi-square-edit-outline text-success"></i></a>-->
                    								<a href="add-city/<?php echo $city2['cityid']; ?>" class="action-icon" title="Edit"> <i class="mdi mdi-square-edit-outline text-success"></i></a>
                    							</td>
                    						</tr>
                    					<?php
                    					        $sn++;
                    					    }
                    					?>

My htaccess code looks like

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^add-city/([0-9]+)$ list-city.php?edt=$1

Please look into and guide.
Regards
Himanshoo

Okay, from the provided information, you want to achieve SEO-friendly URLs such that:

  • Simple pages like about.php should become about
  • Pages with parameters like add-city.php?edt=3 should become add-city/3

Your .htaccess seems mostly correct. Let’s go through it step by step:

  1. The following rule is for removing the .php extension:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

This works by checking if the request is not for a directory (!-d), then checks if the requested filename with .php exists ($REQUEST_FILENAME}.php -f), and if so, internally forwards to that .php file.

  1. The second rule is for transforming the SEO-friendly URL into a proper URL with parameters:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^add-city/([0-9]+)$ list-city.php?edt=$1

This captures URLs that start with add-city/ followed by numbers and internally forwards them to list-city.php?edt=CAPTURED_NUMBER.

However, you mentioned you want the user to be directed to add-city.php for editing, so the rule should point to add-city.php:

RewriteRule ^add-city/([0-9]+)$ add-city.php?edt=$1

Lastly, ensure:

  • Your .htaccess is in the root directory of your website.
  • The Apache mod_rewrite module is enabled.
  • You’ve set the correct permissions for .htaccess.
  • The AllowOverride directive is set to All or at least FileInfo for your directory in your Apache config to allow .htaccess to override settings.

Good Luck

Here’s slightly different ‘read’ on this problem. If you want to do this for content pages, such as city ‘more information’ links, do so. These are the type of things that Search Engines should index and appear in search results.

For something like an edit operation, a Search Engine, which will never appear to be a logged in user, should never ‘see’ the edit links, i.e. if the current user is not logged in, with permission to preform an edit operation, you should not output these on the page. If you did display these edit links to a Search Engine and they are indexed, all leading to a page that returns the same content, e.g. a message that you must be logged in to access the page or a redirect to some other page, you would actually reduce your search engine ranking.

Thanks for reply and guidance.
I changed my code in both the files.

HTACCESS code
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^add-city/([0-9]+)$ add-city.php?edt=$1

CITY code

<a href="add-city/<?php echo $city2['cityid']; ?>" class="action-icon" title="Edit"> <i class="mdi mdi-square-edit-outline text-success"></i></a>

it makes the url like /admin/add-city/8

but displays INTERNAL SERVER ERROR

Regards
Himanshoo

thanks for the reply and guidance.

You are right, sir.
This code is for internal staff only not for frontend.
i just want to know where i am wrong so that i can implement it in frontend.
I had services page at frontend where it redirect to service details page.

Regards
Himanshoo

try

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^add-city/([0-9]+)$ add-city.php?edt=$1 [L,QSA]

The [L, QSA] flags at the end mean:

  • [L]: If the rule matches, don’t process any more rules.
  • [QSA]: Preserve any query strings. Not strictly necessary for your current rule, but can be helpful in more complex configurations.
Sponsor our Newsletter | Privacy Policy | Terms of Service