My desired result is change a file path as a direct decedent from root, where there are N numbers of paths.
For example:
www.host.com/a/b/c/e/f/g/images/1.jpg, where A~G is not always given.
Result:
www.host.com/images/1.jpg
This is what I have so far:
www.host.com/a/images --> www.host.com/images
Using: RewriteRule ^a\/images/$ images/$1 [L]
What I need is a wildcard in front of /images/
Like this: RewriteRule ^(.*)\/images/$ images/$1 [L]
I have also tried: RewriteRule ^(.*)/images/$ http://www.host.com/images [R=301,L]
What is the proper way to write this?
Can you try this rule:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^.+?(/images/.*)$ $1 [L,NC]
RewriteRule ^index\.php$ - [L]
Related
I have a PHP script on a subfolder of my server and it generates url like this:
http://example.com/subfolder//index.php?a=profile&u=username
Instead I would like a SEO friendly url like:
http://example.com/subfolder/profile/username.html
I'm on Apache and mod_rewrite is enabled, so I edited the .htaccess of my subfolder page:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ //index.php?a=$1&u=$2 [L]
But it doesn't work.
This is my full .htaccess code:
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
RewriteRule ^([^/]*)/([^/]*)\.html$ //index.php?a=$1&u=$2 [L]
What's wrong with my code?
Use this rule in /subfolder/.htaccess:
RewriteEngine on
RewriteBase /subfolder/
RewriteRule ^([^/]+)/([^/]+)\.html$ index.php?a=$1&u=$2 [L,QSA,NC]
the current is:
http://www.vitalimaging.com/about.php?page=overview_vision
want to clean the url, and here is my code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^about/([A-Za-z0-9-]+)/?$ about.php?page=$1 [L]
why it's not working?
Your example also has underscore in query string but your regex in RewriteRule doesn't include _. Simplify your regex like this:
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(about)\.php\?page=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteRule ^about/([^/]+)/?$ /about.php?page=$1 [L,QSA,NC]
I want the URL rewrite to result in this:
test.php?site=hi --> www.domain.com/hi
test.php?site=hi&id=1 --> www.domain.com/hi/1
test.php?site=hi&cmd=test --> www.domain.com/hi/test
test.php?site=hi&cmd=test&id=1 --> www.domain.com/hi/test/1
I have tried this, but it doesn't work. I am new at regular expressions, so there might be place for corrections:
RewriteRule ^([a-zA-Z]+)/?+([0-9]+)?/? test.php?site=$1&id=$2
RewriteRule ^([a-zA-Z]+)/?+([a-zA-Z]+)?/?+([0-9]+)?/? test.php?site=$1&cmd=$2&id=$3
How should the RewriteRule be?
Thanks in advance.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([a-z]+)/(\d+)/?$ /test.php?site=$1&cmd=$2&id=$3 [L,NC,QSA]
RewriteRule ^([^/]+)/([a-z]+)/?$ /test.php?site=$1&cmd=$2 [L,NC,QSA]
RewriteRule ^([^/]+)/(\d+)/?$ /test.php?site=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ /test.php?site=$1 [L,QSA]
This doesn't work:
RewriteRule ^([^/]+)([/]?)$ /index.cgi?l=$1 [NC,L]
This doesn't work:
RewriteRule ^([^/]+)/?$ /index.cgi?l=$1 [NC,L]
There's no other rules in the .htaccess file. Here's the complete version:
Options -Indexes
Options ExecCGI
AddHandler cgi-script .cgi .pl .q
ErrorDocument 500 /error500.cgi
ErrorDocument 404 /error404.cgi
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/?$ /index.cgi?l=$1 [NC,L]
This DOES work, but it's not what I want.
RewriteRule ^([^/]+)/([^/]+)$ /index.cgi?l=$1&a=$2 [NC,L]
I want both the first slash and second directory to be optional. Why won't the question mark match 0 or 1 instances like it's supposed to? I am freaking here...
By using a $, you're specifying that it's the end of the text, so that won't match anything after a /. (in regex, ^ specifies the beginning of a string and $ specifies the end)
You could remove the $, then it will make the second parameter optional - that sounds like what you're looking for.
Try this to see if your mod_rewrite is working correctly:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [R,L]
I'm having trouble with this reg expression which i belive is correct, but it is not working.
What im trying to do is redirect bunch of urls containing a specific string like this:
http://www.example.com/**undesired-string**_another-string to http://www.example.com/**new-string**_another-string
and
http://www.example.com/folder/**undesired-string**/another-string to http://www.example.com/folder/**new-string**/another-string
So i have this code in the .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule (.+)+(undesired-string)+(.+) $1new-string$2 [R=301,L]
</IfModule>
This should replace ANY undesired-string in any url to new-string, but it is not working, any idea why ?
Thank you
Marwen: Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)undesired-string(.*)$ yoursite.com/$1new-string$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^www.yoursite.com$ [NC]
RewriteRule ^(.*)$ yoursite.com//$1 [L,R=301]
</IfModule>
In your 'updated' code in the comments above, you had it applying the rewrite condition to the undesired-string... So if the actual file or directory was valid it would not rewrite...
Doing this though will always rewrite the undesired-string with new-string - even if its a file name... If that is fine or what you want then all you had to do was move your rewrite conditions to below the rewrite rule...
also.. Just an fyi.. If everything is on yoursite.com you dont need to list yoursite.com
i.e.
yoursite.com/$1new-string$2
just needs to be
/$1new-string$2
which does the same thing: rewrites to the base directory of yoursite.com
now if they are going from mysite.com to yoursite.com then you woulud want to include the domain name because you are redirecting across domain names
Edit: You may also want to use:
[QSA,L,R=301]
instead of just [L,R=301]
Your regex is not really correct. Try:
RewriteRule ^(.*)undesired-string(.*)$ $1new-string$2 [R=301,L]
Or if this doesn't work, try:
RewriteRule ^(.*)undesired-string(.*)$ http://yoursite.com/$1new-string$2 [R=301,L]
Explanation:
^ marks the beginning; $ marks the end; the first (..) goes to $1, the second (..) goes to $2 and so on; * is 0 or more chars; + is 1 or more chars.
To answer my own question. Laravel already redirects the trailing slashes. Problem was that Laravel was installed into a sub-directory. I added the location of the sub-directory to the redirect. My location in this case is: "/lumen/public/". See the fixed htaccess below.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /lumen/public/$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>