I currently have an entire website running on PHP & GET variables.
My links look like this at present
http://www.example.co.uk/brochure.php?cat_path=24
And occasionally it has a second variable in the url...
I actually would like my URLs to look like this: http://www.example.co.uk/Concrete_Fence_Posts
So my questions is simply, how do I go about rewriting urls without breaking my GET variables.
Thanks.
For better search search engine optimisation have URLs like stackoverflow -
There is something like search engines do not store whatever is after the
?
in your URLs. This endingconcrete-fence-posts
also helps users to identify the page in the browser address bar. But, you cannot allow certain special characters if the?
is not used in the URL. Characters like/
,%
will break the URL and\
,#
,+
will not appear.Updates
Do you want to move completely from ID passing to name passing? I am not sure about how good that will be, but I have heard that a few well known sites have URLs like that. But, you may have to do many changes depending on your project (how many modules you have). The stackoverflow style URL will, however, retain the ID based item fetching.
Further Updates
You can try this rewrite rule to have both ID and name in the URL (stackoverflow style):-
I have not tested it though.
Another important thing, I see that your code enters
brochure.php
directly and there is no common file (common controller) which is executed in case of all the modules. This approach can have many drawbacks, particularly if your's is a medium to large application. It is always better to execute a commonindex.php
file and include the neededbrochure.php
file there so that you can perform all those common logics (needed in all your modules) in thisindex.php
file. I have a common index.php file and the following rewrite rules do the same:-Note:- The first block of rewrite rules hides the index.php from the URL and a visitor does not know that my application runs in PHP.
Finally I have URLs like this:-
Hope this helps,
Sandeepan
Assuming your parameters are always named
cat_path
andproduct_id
(if it exists) you can do something like this:Your URLs would then be in one of these formats:
For example:
Edit: I see you want to use product names in the URL. In that case, your PHP scripts will need to be able to take a name as a parameter and look up the ID. You should continue to accept the ID directly so as to not break existing links. Then your rewrite rule would look like this:
And http://www.bentinckfencing.co.uk/Concrete_Fence_Posts would rewrite to http://www.bentinckfencing.co.uk/brochure.php?name=Concrete_Fence_Posts.