Rewriting the Friendly urls without php extension [duplicate] - .htaccess

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed 3 years ago.
I'm using Wordpress to build this website but suddenly ran across some issues with SEO.
Currently I have link like :
cart/?bino=cart&gid=3
But what i want to achieve is:
cart/service/x-service/
I know this is possible if i would have cart.php?bino but this case is a bit different. Would like to hear your thoughts on this.

As mentioned in the comments your question is vague. I can only make some suggestion here for what apepars to me plausible given the little information you provided. You probably have to tweak things for your specific situation because of that.
I assume that the gid argument can only assume numerical values and that the bino argument can take arbitrary "words" as value. If so this probably is what you are looking for, it takes care of both sides: redirection the "old" URL scheme and internally rewriting the new scheme:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)bino=(\w+)(?:&|$)
RewriteCond %{QUERY_STRING} (?:^|&)gid=(\d+)(?:&|$)
RewriteRule ^/?cart.php/?$ /cart/%1/%2 [R=301,QSD]
RewriteRule ^/?cart/(\w+)/(\d+)/?$ /cart.php?bino=$1&gid=$2 [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...
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 implementation 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).

Related

Having trouble redirecting URL when query string is present in .htaccess

I updated my website, and I'm attempting to make sure a bunch of old URLs redirect to the correct page. Some of my links are working fine, while others aren't.
Example:
Redirect 301 /compare-suites/ /en/our-products/compare-suites
Redirect 301 /compare-suites/?lang=fr /fr/nos-produits/comparaison-des-suites/
The first redirect works perfectly, but the second one redirects to /en/our-products/compare-suites?lang=fr, ignoring my second rule.
The alias module which offers the Redirect directive is unable to handle the query string inside a URL. That is clearly stated in the documentation. Along with the hint to use the rewriting module instead.
I strongly recommend that you start reading the documentation of the utilities you use:
Alias module: https://httpd.apache.org/docs/current/mod/mod_alias.html
Rewrite Module: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
As typical for OpenSource they are of excellent quality and come with great examples.
This would be the approach you are probably looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)lang=fr(&|$)
RewriteRule ^/?compare-suites/?$ /fr/nos-produits/comparaison-des-suites/ [QSD,R=301,END]
RewriteRule ^/?compare-suites/?$ /en/our-products/compare-suites [R=301,END]
Note that the more specialized exception has to be implemented before the more general fallback, so further up in the configuration file.
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...
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 implementation will work likewise in the http servers host configuration or inside a distributed 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 distributed 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 distributed configuration files (".htaccess"). Those distributed 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).

Problem using regex to adjust query strings in htaccess redirectmatch

There's income traffic to my website with a pattern like this:
/index.php?title=xxx&somethingelse=yyy
And I need to turn it into this pattern:
/somefolder/xxx/
I've tried different things, such as:
RedirectMatch 301 ^/index.php\?title=([^&]*).*$ https://example.com/somefolder/$1/
The above code, and many other alternatives that I've tried based on online posts and articles didn't work. Maybe I'm just making a silly mistake somewhere; it's been half a day that I'm trying without success!
I appreciate your help.
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)title=([^&]*)(?:&|$)
RewriteRule ^/?index\.php$ https://example.com/somefolder/%1/ [R=301]
RewriteRule ^/?somefolder/(.+)/?$ https://example.com/index.php?title=$1 [END,QSA]
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...
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).

.htaccess Redirect Subdirectories to Query Strings

I've been searching for a while and can't find an answer to my question.
I'm working on a site with multiple locations and for SEO purposes want to be able to use the location in the URL but have it redirect to a common page for all of them.
For example, I want the following URLs to redirect from:
www.mysite.com/newyork/page1
www.mysite.com/newyork/page2
to:
www.mysite.com/page1?location=newyork
www.mysite.com/page2?location=newyork
While keeping the URL the same for vanity purposes.
There are also some URLs which would need to redirect additional query strings based on subdirectories, for example:
www.mysite.com/newyork/product/productname
becomes
www.mysite.com/product?location=newyork&name=productname
Is there a way to achieve this?
Certainly that is possible, actually it is straight forward. So the question is why none of all the answers here on SO provided any help for you. Without you specifying more details, so why all those answers did not help, so what specific issue you ran into, all we can do is provide one more answer along the line of all those previous ones:
RewriteEngine on
RewriteRule ^/?([^/]+)/([^/]+)/?$ /$2?location=$1 [END,QSA]
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/?$ /$2?location=$1&name=$3 [END,QSA]
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).

.htaccess redirect to level up folder with variable

URL /%variable1%/plan/%variable2%/ need redirect to /%variable1%/
I tried:
RewriteRule ^([^/]+)/?(.*) /$1/plan/$2 [L]
Returns a 500 error
Please tell me how to do it in Apache
Your question is a bit vague, but this should do what you actually ask:
RewriteEngine on
RewriteRule ^/?([^/]+)/plan/[^/]+/?$ /$1/ [R=301,END]
I am not convinced though that this is what you really want to achieve, since it is uncommon and you will actually lose the information held in what you referred to as "variable2".
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...
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).

Remove ending from URLs using .htaccess

I would like to remove all endings on some URLs that we use on our website. For some reason, lots of URLs get indexed even though they are blocked by robot.txt
Just checked google search console today "Indexed, though blocked by robots.txt" - 1,200+ URLs
they are all in the same form
https://www.ifootpath.com/display-ifootpath-walk?walkID=11470&username
and they should be
https://www.ifootpath.com/display-ifootpath-walk?walkID=11470
the end number will change for each URL (Walking guide) so I guess I would just like to remove '&username'
Thank you
As already mentioned in the comments to the question you need two steps here:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^walkID=(\d+)&
RewriteRule ^/?display-ifootpath-walk$ /display-ifootpath-walk/%1 [R=301]
RewriteRule ^/?display-ifootpath-walk/(\d+)$ /display-ifootpath-walk?walkID=$1 [END]
That rule set will work likewise in the http servers host configuration and in a dynamic configuration file (".htaccess" style file) inside the DOCUMENT_ROOT of that host.
Please note that I did not test those lines but only wrote them down as I saw fit. But I do hope I did not make any silly typo or logical mistake...
If you receive a http status 500 ("internal server error") with those rules then chances are that youoperate a very old version of the apache http server. In that case try replacing the [END] flag with the [L].
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).

Resources