I have a ReWrite rule that has 2 parameters, but the nav parameter shows a / before because of the URL.
RewriteRule ^([^/]+)(/[a-zA-Z0-9-/_]+)?$ index.php?pagename=$1&nav=$2 [L,QSA]
Is there a way I can remove the slash in the htaccess code?
Put The slash / out of capturing group ^([^/]+)(/[a-zA-Z0-9-/_]+)?$ like this ^([^/]+)/([a-zA-Z0-9-/_]+)?$ .
Have it like this:
RewriteRule ^([^/]+)(?:/([\w/-]+))?$ index.php?pagename=$1&nav=$2 [L,QSA]
(?:...)? is an optional non-capture group
([\w/-]+) inside this non-capture group will populate 2nd capture group
Related
Google has indexed some URLs with empty value - space - at the end of each URL, I want to make a permanent redirect via .htaccess here is my code but it did not redirecting :
RewriteRule ^/profile/userx/([a-zA-Z])+/[a-zA-Z]+/[a-zA-Z]/'%20'$ https://mywebsite.net/profile/userx/([a-zA-Z]+)/[a-zA-Z]+/[a-zA-Z]/ [R=301,L]
How to say any URL with space at the end redirect it to that one without space?
You may use this rule as your topmost rule to remove 1+ whitespace from the end of URL:
RewriteEngine On
RewriteRule ^(.*)\s+$ /$1 [L,NE,R=301]
# other rules go below this line
\s+$ matches 1+ whitespace at the end of a URI and .* matches anything before whitespaces.
I'm trying to work on making a new site and I want to be able to mirror a site. Below is an example:
User visits: https://example.com/items/{some child folder}
User sees this file mirrored: https://example.com/items/listing.php
I want user to be able to see that file, but, when doing so, it don't want it to redirect. Any ideas?
UPDATE
I found a solution to the above problem. However, I need another question fixed. How would I stop the file listing.php in the /products folder from following the redirect?
RewriteEngine On
RewriteRule ^products/(.*) index.php?name=$1 [NC,L]
How would I stop the file listing.php in the /products folder from following the redirect?
RewriteRule ^products/(.*) index.php?name=$1 [NC,L]
Be more specific in the regex. If your products don't contain dots in the URL-path then exclude dots in the regex. For example:
RewriteRule ^products/([^./]+)$ index.php?name=$1 [L]
The above assumes your product URLs are of the form /products/<something>. Where <something> cannot consist of dots or slashes (so naturally excludes listing.php) and must consist of "something", ie. not empty.
Unless you specifically need the NC flag then this potentially opens you up to duplicate content.
If you want to be explicit then include a condition (RewriteCond directive):
RewriteCond %{REQUEST_URI} !^/products/listing\.php$
RewriteRule ^products/([^/]+)$ index.php?name=$1 [L]
The REQUEST_URI server variable contains the root-relative URL-path, starting with a slash. The ! prefix on the CondPattern negates the regex.
Or, use a negative lookahead in the RewriteRule pattern, without using a condition. For example:
RewriteRule ^products/(?!listing\.php)([^/]+)$ index.php?name=$1 [L]
Reference:
https://httpd.apache.org/docs/current/rewrite/intro.html
https://httpd.apache.org/docs/current/mod/mod_rewrite.html
I need to remove /photo/26/ from this url http://localhost/photo/26/h-house
I already have removed index.php from all links.
You can do something like the following at the top of your .htaccess file using mod_rewrite to redirect /photo/26/h-house to /h-house, effectively removing /photo/26 from the URL:
RewriteRule ^photo/26/h-house$ /h-house [R=302,L]
Or, to avoid repetition, you can do the following instead:
RewriteRule ^photo/26(/h-house)$ $1 [R=302,L]
$1 is a backreference that contains /h-house from the capturing group in the RewriteRule pattern.
I would like to apply the following htaccess rule only if there is a value after the forward slash of /events/
If I visit /events/ (Don't do anything. DO NOT apply the htaccess rule)
But if I visit /events/this-can be-anything/ (Apply the htaccess rule below)
RewriteRule ^events/(.*) /lp/events/index.php [L]
This is obviously not working.
You need to change your regex pattern (.*) to (.+) to match at least one character after the uri.
RewriteRule ^events/(.+)$ /lp/events/index.php [L]
I am currently having a problem with my index url rewrite in my .htaccess file, I know if I use
RewriteRule ^profile/([^/]*)/?$ /profile.php?x=$1 [L]
I would be able to use www.example.com/profile/get or www.example.com/profile/get/ (with or without trailing slash)
But I would like www.example.com/get what I have so far is
RewriteRule ^([^/]*)\/$ /index.php?x=$1 [L]
But if I put a ? before the $ it errors any answers welcome
Making the trailing slash optional will lead to an infinite loop, since [^/]* will match anything that doesn't include a /, ie it would also match index.php?x=get
You can avoid this by making the rule apply conditionally, for example by testing the reqeust URI:
RewriteCond %{REQUEST_URI} !^/index\.php.*
RewriteRule ^([^/]*)\/?$ /index.php?x=$1 [L]
That way the rule can only apply in case the request URI doesn't start with /index.php