2

I have this site made from the developer in PHP and now all the pages have this URL structure,

websitename.com/software.php?name=product_name

The only thing which changes every time on various pages on site is only the "product name" such as:

websitename.com/software.php?name=product_name1

websitename.com/software.php?name=product_name2

websitename.com/software.php?name=product_name3

etc

I only want to have URL like this:

websitename.com/product_name1

websitename.com/product_name2

websitename.com/product_name3 and so on..

can you please tell me what code should I be adding on my htaccess in order to get such URL structure.

  • 1
    Possible duplicate of [htaccess Redirect directory name to parameter](https://stackoverflow.com/questions/21662957/htaccess-redirect-directory-name-to-parameter) – showdev Aug 11 '19 at 04:17

2 Answers2

0

For websitename.com/any to websitename.com/software.php?name=any use this rules in top of yours .htaccess file

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /software.php?name=$1 [L]
</IfModule>

But more usefull URL structure (software only for products) would be websitename.com/software/any-product-name to websitename.com/software.php?name=any-product-name

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^software\/(.*)$ /software.php?name=$1 [L]
</IfModule>

For SEO friendly URLs use "-" instead of "_" in product names.

JarekBaran
  • 1,311
  • 2
  • 7
  • 12
  • its working fine mate but both of the links are available, can you tell me code through which I can 301 redirect the old URL structure to new URL structure, thanks – Andrew Hales Aug 12 '19 at 16:10
0

This should point you into the right direction:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /software.php?name=$1 [END]

In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.

This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).


UPDATE: In the comment below you ask in addition to redirect a request to the original, not rewritten URL. Here is an extended version doing that:

RewriteEngine On

RewriteCond %{QUERY_STRING} (?:^|&)name=([^&]+)(?:&|$)
RewriteRule ^/?software\.php$ /%1 [R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /software.php?name=$1 [END]

It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • not working mate, it's giving 500 server error, tried with [L] as well at the end but still no url structure changes – Andrew Hales Aug 12 '19 at 15:48
  • The below code is working but can you tell me code through which the old url structure can be 301 redirected to the new url structure in the meantime, thanks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /software.php?name=$1 [L] – Andrew Hales Aug 12 '19 at 16:45
  • The Updated code is still not working, i am still having the same old url, no redirections are there. – Andrew Hales Aug 12 '19 at 17:25
  • That means that the rules to not get applied at all. Please specify the _exact_ URL you try and also where you implemented those rules. How did you make sure that such dynamic rules get evaluated _at all_ ? – arkascha Aug 13 '19 at 12:02
  • the project is under going so maybe i can't keep that code on htaccess always but for you to look at it i will setup it somewhere else, if possible can we chat about it on facebook? i will really appreciate it – Andrew Hales Aug 14 '19 at 02:13
  • Sure, we can have a direct discussion, but not on Facebook, I don't use that one. SO here offers chatrooms which are perfectly adequate for that. I can only attend to this in the evening, though, since I am busy at work currently. You create a chat room and post the link in a comment here. I will try to enter that room this evening, say around 8ish central european time. Is that fine with you? – arkascha Aug 14 '19 at 09:40
  • Hm, you did not create a room? Why not? OK, I did it myself, will look into it from time to time. Just write, I will pick you up. https://chat.stackoverflow.com/rooms/197962/andrew-arkascha – arkascha Aug 14 '19 at 19:27
  • i am really sorry for such delay response I check your chat room link but its saying you must have 20 reputation on stackoverflow to talk there – Andrew Hales Aug 16 '19 at 03:29
  • please if not facebook then create your preferable chat room, i,ll be waiting – Andrew Hales Aug 16 '19 at 04:07
  • There is no way around the 20 points minimum limit to be able to talk in chat rooms. That limit does make sense, I did not realize you do not yet have those point. I upvoted your question, but that still only leaves you with 11 points. Unconvenient in this situation, any way you want to try to gain those few points? . I do not use facebook for various social and political reasons, sorry for that. – arkascha Aug 17 '19 at 11:43
  • i understand, m trying to get it to 20reputations as well. Thing is i totally lost access to my old account in here and that is why i had to make a new one recently, – Andrew Hales Aug 17 '19 at 16:18
  • Hey, the 301 redirection issue has been resolved, thankyou so much anyways, cheers – Andrew Hales Aug 18 '19 at 17:17