I am trying to write rules in .htaccess file.
I Wrote the rule like this:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)&dgid=([0-9]+)$
RewriteRule destination_content-id-(.*)-dgid-(.*)\.htm$ destination_content.html?id=$1&dgid=$2 [L]
restarted the server.
Before it is having the following rule.
RewriteEngine on
# Parse out basename, but remember the fact.
RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes]
# Rewrite to document.phtml if exists...
RewriteCond %{REQUEST_FILENAME}.phtml -f
RewriteRule ^(.*)$ $1.phtml [S=1]
# ...else reverse the previous basename cutout.
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $1.html
it works fine.
but my rule not working.
Could you please help me in solving the issue.
Thanks,
Srilu
Leave out the RewriteCond (It does not match the RewriteRule).
You'll just need the RewriteRule and I guess you want it to look like this:
RewriteEngine on
RewriteRule destination_content-id-([0-9]+)-dgid-([0-9]+)\.htm$ destination_content.html?id=$1&dgid=$2 [L]
Related
Hi i have link directories like this:
www.example.com/a-letter/a-1
www.example.com/a-letter/a-2
www.example.com/a-letter/a-3
i removed a-letter folder name from the link with this code:
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]
and i want to redirect all a-1 a-2 and a-3 files under the a-letter folder to like below with one htaccess code
www.example.com/a-1
www.example.com/a-2
www.example.com/a-3
because i am using this codes for each link and i want to use one code for this. How can i specify the code for all files
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^a-1$ /a-letter/a-1.html [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^a-2$ /a-letter/a-2.html [L]
</IfModule>
I will be grateful if you could help me.
This probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(a-1|a-2)/? /a-letter/$1.html [END]
An alternative would be that, if the name scheme is some thing like "a-.html":
RewriteEngine on
RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?a-(\d+)/? /a-letter/a-$1.html [END]
I personally do not use those <IfModule mod_rewrite.c> conditions ... Yes, the might formally prevent an internal server error. But what does that help?
Operating your site without those rules getting applied most likely does not make much sense.
You know whether you have installed the rewriting module or not. And you are most likely not going to change that frequently.
UPDATE:
In the comments you asked another question (again: please open a separate question in future for separate questions...). I would suggest such implementation:
RewriteEngine on
RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]
RewriteRule ^/?comments/a/(.*)(/|\.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?a-(\d+)/? /a-letter/a-$1.html [END]
RequestCond %{DOCUMENT_ROOT}/comments/a%{REQUEST_URI}.html -f
RewriteRule ^/?(.*)/? /comments/a/$1.html [END]
Your directives look fine, it might be that the issue you ran into is the order of directives. Always keep in mind that the directives are processed from top to bottom. Your last RewriteRule is very generic, it will match anything . Always keep more generic rules at the bottom, more precise exceptions further atop. And always try to modify rules such that they are less generic:
So instead of
RewriteRule ^/?(.*)/? /comments/a/$1.html [END]
maybe something like that
RewriteRule ^/?(\w*)/? /comments/a/$1.html [END]
is possible?
I have:
mydomain.com/folder-name/segment1/segment2
I want to change it to:
mydomain.com/segment1/segment2
using a 301 redirect.
I've tried:
RewriteCond %{REQUEST_URI} !^/test/.*$
RewriteRule ^(.*)$ /test/$1 [L]
but its not working
here is my htacess file:
# #AddHandler application/x-httpd-php53 .php .php5 .php4 .php3
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/b1/.*$
RewriteRule ^(.*)$ /b1/$1 [R=301,L]
The answer for the first part of the question should be like this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? $2/$3 [R=301,L]
The second code that you've tried is the opposite of what you're asking for initially. This line matches anything not starting with /test/:
RewriteCond %{REQUEST_URI} !^/test/.*$
This line says take everything and rewrite it to the /test/ directory:
RewriteRule ^(.*)$ /test/$1 [L]
So together anything that's not in the test directory is being rewritten to the test directory.
If you're trying to specifically remove the word test then you would remove the ! symbol in your attempt to create a match. Since you already know it's called test there's no need to even make Apache perform this look for 'test' because Apache handles the RewriteCond statement after the RewriteRule (rather unintuitively).
RewriteCond %{REQUEST_URI} ^/?test
You can specialize the rewrite rule like this (I've added [QSA] to catch any query strings:
RewriteRule ^test/([^/]+)/([^/]+)/? $1/$2/ [R=301,L,QSA]
Simply change your code to:
RewriteRule ^test/(.*)$ /$1 [R=301,L,NC]
I have url
http://www.url.com/business/index.php?biz=andrewliu
I'm trying to accomplish so it will be
http://www.url.com/business/andrewliu
I tried to have this:
RewriteRule ^/?([a-z0-9_-]+)/?$ /index.php?biz=$1 [L]
or
RewriteRule ^/?([a-z0-9_-]+)/?$ /business/index.php?biz=$1 [L]
doesn't work?
Help me?
Edit:
I have this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
prior to the rewriterule
That depends in which directory your .htaccess file is.
For the root directory try this one:
RewriteRule ^/?business/([a-zA-Z0-9_-\.]+)/?$ /business/index.php?biz=$1 [L]
If your .htaccess file is in the business directory that of your statments should work fine:
RewriteRule ^/?([a-zA-Z0-9_-\.]+)/?$ /index.php?biz=$1 [L]
RewriteRule ^business/([_0-9a-zA-Z-]+)/?$ business/index.php?b=$1 [L]
It's not clear from your examples whether you want "biz" or "business" or "b". I'm taking a guess that you want "biz", in which case your rule should be:
RewriteRule business/(.*) /index.php?biz=$1 [L]
Or maybe you want:
RewriteRule ([^\/]*)/(.*) /index.php?$1=$2 [L]
Ok I have the following .htaccess and it works however I can seem to change it to the way I need it.
here is the current .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|system|images|tpl|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php?tpl=$1 [L]
Options +FollowSymlinks
I need to add the following
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&tpl=$2 [L]
So i am wondering how do I get that rule to work? as when I add it it does not.
So if you want to add a rule that will handle all requests except the first one that matches the RewriteCond And those would be split into /?
I think something like this would work. Please note that I changed the $1 to ${REQUEST_URI} in your rewrite condition
RewriteEngine on
RewriteCond ${REQUEST_URI} !^(index\.php|admin|system|images|tpl|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php?tpl=$1 [L]
RewriteRule ^([^/]*)/?(.*)?$ /index.php?cat=$1&tpl=$2 [L]
Options +FollowSymlinks
I have the following rules in .htaccess. Unfortunately, it does not work due to the last rule (everything else works fine). Why?
Options -Indexes
RewriteEngine On
RewriteRule ^(cdn) - [L]
RewriteRule ^admin/(.*)$ backend_0.0.1/index.php/$1 [QSA,L]
RewriteRule ^css/(.*)$ frontend_0.0.1/css.php/$1 [NC,QSA,E=no-gzip:1,L]
RewriteRule ^js/(.*)$ frontend_0.0.1/js.php/$1 [NC,QSA,E=no-gzip:1,L]
RewriteRule ^(.*)$ frontend_0.0.1/index.php/$1 [QSA,L]
If I replace the last line by:
RewriteRule ^(.*)$ frontend_0.0.1/index.php?q=$1 [QSA,L]
Then it suddenly starts to work but previous rules are skipped and only this last rule is applied. But I need rules to stop rewriting once the first one mathches.
You need to exclude the destinations you are redirecting to:
RewriteCond $1 !^(backend_0\.0\.1|frontend_0\.0\.1)/
RewriteRule ^(.*)$ frontend_0.0.1/index.php/$1 [QSA,L]