How to apply a rule only to the root directory (where it's placed)?
How about putting:
RewriteCond %{REQUEST_URI} /([^/]*)
before any of your rules. That will match on any number of characters that are not a slash.
I would use the following
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/[^/]*(\?.*)?$
# your rewrite code here
Related
How to create redirection rule in .htaccess for:-
earlier we have 400+ paths like.
/blog/some-path1
/blog/some-path2
but now we have paths like.
/some-path1
/some-path2
How to create single line redirection rule for above redirection without write multiple redirections.
tried below rule but not working
RewriteEngine on
RewriteBase /
RewriteRule ^blog/(.*)$ /$1 [L,NC,R]
Try with below rule, I am assuming you know the paths are defined correctly, clear cache beforehand.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^blog/(.+)
RewriteRule ^ /%1 [R=301,L]
This is my .htaccess but I would also like to exlude the root directory ('mysite.com' slash nothing) from the rule.
RewriteCond %{REQUEST_URI} !(wp-admin|index.php)
RewriteRule ^([a-zA-Z0-9\D]+)$ /index.php?username=$1 [L]
Any thoughts? :)
To exclude the root dir, put this condition above your rule
RewriteCond %{REQUEST_URI} !^$
I am trying to make a user system, where the URL is like:
test/user/[name]
It all works, but if you try:
test/user/name/something
It doesn't work. Is there any way to remove the slashes from that certain point, so that:
test/user/name/something
will go to:
test/user/name
How do I edit this, so it works as mentioned above:
RewriteEngine on
RewriteCond %{REQUEST_URI} user/(.*)
RewriteRule user/(.*) something/test/user.php?user=$1
You can place this rule as your first rule in your root /test/.htaccess:
RewriteEngine on
RewriteBase /test/
RewriteRule ^(user/\w+)/.+$ /$1 [L,NC,R]
RewriteRule ^user/(.+)$ user.php?user=$1 [L,QSA]
I needed to redirect "example.com/image/img.jpg" to "example.com/view/img.jpg"
Therefore I've used
RewriteRule (^|.*?/)image/(.*)$ /$1view/$2 [R=302,L,NC]
But, still if I try to visit "example.com/image/img.jpg" it won't redirect. What am I doing wrong?
Thank you.
I needed to redirect "example.com/image/img.jpg" to "example.com/view/img.jpg"
Apparently the above examples are not accurate as your rule shows directories image and view can be at any level in the corresponding URL directory structures.
If that's the case, you may try this in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)?image/([^.]+)\.jpg [NC]
RewriteRule .* /%1view/%2.jpg [R,L,NC]
In case image and view directories are indeed at the first level as described in the examples, replace the last 2 lines with this one:
RewriteRule ^image/(.*) /view/$1 [R,L,NC]
Replace [R,L,NC] with [R=301,L,NC] for permanent redirection or with [L,NC] for internal mapping.
I'd like to map:
mywebsite.com/users/ -> mywebsite.com/users/users.php
mywebsite.com/users -> mywebsite.com/users/users.php
mywebsite.com/users/username -> mywebsite.com/users/user.php?name=username
At present, I'm using this .htaccess in the users directory:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_])*$ user.php?name=$1 [L]
RewriteRule ^.*$ users.php [L]
However, it never generates the user.php?name=$1 URL.
Why doesn't it work?
Here are the rules (place in .htaccess in website root folder. If placed elsewhere some tweaking is required):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# 1
RewriteRule ^users/?$ /users/users.php [L]
# 2
RewriteCond %{REQUEST_URI} !^/users/users?\.php
RewriteRule ^users/([^/]+)$ /users/user.php?name=$1 [QSA,L]
Rule #1 will match fist 2 URLs of yours.
Rule #2 will work with specific user mapping. It will ensure that it does not rewrite already rewritten URLs.
UPDATE:
If you want to place it into .htaccess file in /users/ folder, then this URL mywebsite.com/users (without trailing slash) most likely will not work.
But in any case -- here are the rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# 1
RewriteRule ^$ users.php [L]
# 2
RewriteCond %{REQUEST_URI} !^/users/users?\.php
RewriteRule ^([^/]+)$ user.php?name=$1 [QSA,L]
It looks to me like it is applying both rules in sequence: the url matches the first rule, so it adds user.php?name=$1, and then it matches the second rule, so it replaces the string with users.php. If that is the problem, then for your particular case you could fix it by replacing the second regular expression with ^/?$.