Looked for an answer but couldn't find one that solved my specific issue.
The Manager of my website is located under /manager/php an and want to remove the "php" from the URL.
For example https://example.com/manager/php/topkek.php would become https://example.com/manager/topkek.php.
Tried this but it gives an internal server error probably because of infinit redirects:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^manager/(.+)$ /manager/php/$1 [L]
Your rule causes an infinite rewrite loop as your pattern ^manager/(.+)$ also matches the substitution string /manager/php/$1 . You need to fix it so that the pattern can not match the destination path. You can exclude the /php path in your regex to fix the error.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^manager/((?!php).+)$ /manager/php/$1 [L]
Or you can use a RewriteCond directive to exclude the destination path
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/manager/php
RewriteRule ^manager/(.+)$ /manager/php/$1 [L]
Related
Can you any one see anything wrong with the following apache rewrite rule:
This is in my .htaccess file inside a folder called "text" a subdirectory of localhost/lombardpress I have the following rule
Options +FollowSymlinks
RewriteEngine on
RewriteRule ([^/]+) /textdisplay.php?fs=$1 [NC]
I was expecting this input:
http://localhost/lombardpress-dev/text/lectio1
to rewrite to this:
http://localhost/lombardpress-dev/text/textdisplay?fs=lectio1
But instead I get a 404 error.
The requested URL /textdisplay.php was not found on this server.
It looks to me like the RewriteRule has re-written the address but not as I intended - so there must be something wrong with my regular expression.
Let me know if I can provide further information.
Try this
Options +FollowSymlinks
RewriteEngine on
RewriteBase /lombardpress-dev/text/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+) textdisplay.php?fs=$1 [NC]
With that rewrite cond you wont redirect textdisplay.php to itself again.
The problem is that [^/]+ matches all but / so it matches even textdisplay.php
Remove leading slash in target URL:
Try this code:
Options +FollowSymlinks
RewriteEngine on
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ textdisplay.php?fs=$1 [NC,L,QSA]
Reference: Apache mod_rewrite Introduction
Get rid of the / in front of the target:
RewriteRule ([^/.]+) textdisplay.php?fs=$1 [NC]
# no slash here ----^
Based on code found here: remove multiple trailing slashes mod_rewrite
I have the following htaccess
Options +FollowSymLinks
DirectorySlash Off
RewriteEngine on
RewriteOptions inherit
RewriteBase /
#
# remove multiple slashes from url
#
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]
#
# Remove multiple slashes anywhere in URL
#
RewriteCond %{THE_REQUEST} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
Yet i found out the G-Bot has crawled this url: http://www.example.com/aaa/bbb/////////bbb-ccc/bbb-ddd.htm. (aaa, bbb, ccc, ddd, are keywords in url, not to be taken litraly - i jut show the pattern of the url)
Testing the above url in by live server i found out that the slash removal does not work.
Anyone can offer any tips or improvement to the the existing code? Thank you
EDIT 1
#Sylwester provided the following code
# if match set environment variable and start over
RewriteRule ^(.*?)//+(.*)$ $1/$2 [E=REDIR:1,N]
# if done at least one. redirect with 301
RewriteCond %{ENV:REDIR} 1
RewriteRule ^/(.*) /$1 [R=301,L]
It is not working either. I still see the ////// inside the url.I have put this set of rules at the very top of my htaccess file, right below the " RewriteBase /", so as not to be affected by other rules, yet... nothing.
Any other suggestion?
Per directory and .htaccess is tricky since apache actually have removed redundant slashed for us. Eg. there is no match for //+ anymore so we check the %{REQUEST_URI} since it has the original URI while the rewrite rule need to match anything:
# NB: Only works for per directory and .htaccess
# Needs "AllowOverride All" in global config for .htaccess
RewriteEngine On
RewriteBase "/"
Options +FollowSymlinks
# Check if the REQUEST_URI has redundant slashes
# and redirect to self if it has (which apache has cleaned up already)
RewriteCond %{REQUEST_URI} //+
RewriteRule ^(.*) $1 [R=301,L]
If you can add global config I would have prefered this in the virtual host instead:
RewriteEngine On
# if match set environment variable and start over
RewriteRule ^(.*?)//+(.*)$ $1/$2 [E=REDIR:1,N]
# if done at least one. redirect with 301
RewriteCond %{ENV:REDIR} 1
RewriteRule ^/(.*) /$1 [R=301,L]
i tried this to rewrite url,and worked perfectly
Options +FollowSymLinks
RewriteEngine on
RewriteRule download-id-(.*)\.htm$ download.php?id=$1
but when i tried to rewrite download.php?id=xx to download.php/id/xx by the following:
Options +FollowSymLinks
RewriteEngine on
RewriteRule download/id/(.*)/ download.php?id=$1
RewriteRule download/id/(.*) download.php?id=$1
i got an 404 - Not found! error
what's the problem
i used this good tool to generate the syntax
http://www.webconfs.com/url-rewriting-tool.php
You need to make it:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^download/id/([0-9]+)\/?$ download.php?id=$1
the /? means it can have a trailing slash or not, and the ^ and $ start and end the search string.
I have created a site in a subdirectory and would like the site to appear as if it's in the root.
I used the below Mod_Rewrite code to get the site root to redirect to the subdirectory, but I would like the folder the files are held in to not appear.
currently: www.example.com/sitefiles/content/
Would like: www.example.com/content
Thanks
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^$ /sitefiles/ [L]
RewriteRule (.*) /sitefiles/$1 [L]
If it needs to be done via .htaccess and mod_rewrite, then use this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/sitefiles/
RewriteRule (.*) /sitefiles/$1 [L]
Since you do not explicitly specify the page when website root will be hit, then there is no need for this line: RewriteRule ^$ /sitefiles/ [L]
You need to add condition for your main rewrite rule to prevent rewrite loop (when already rewritten URL gets rewritten again): RewriteCond %{REQUEST_URI} !^/sitefiles/
Well, the directory should have no reason to appear in any listing, and that mod_rewrite you posted should allow you to do as you said. Still, I would go about it a different way:
Options +FollowSymLinks +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^$ sitefiles/ [L]
RewriteRule ^(.+)$ sitefiles/$1 [L]
Hope this works.
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 ^/?$.