I have this mod-rewrite in my htacces which enables some clean urls;
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
### LANGUAGE REDIRECT RULES start
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(en|nl)-?()?\/(.*\/?)$ index.php?language=$1®ion=$2&symphony-page=$3&%{QUERY_STRING} [L]
### LANGUAGE REDIRECT RULES end
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ ./index.php?symphony-page=$1&%{QUERY_STRING} [L]
</IfModule>
Now I would like to also force www, so I extended the code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteBase /
### LANGUAGE REDIRECT RULES start
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(en|nl)-?()?\/(.*\/?)$ index.php?language=$1®ion=$2&symphony-page=$3&%{QUERY_STRING} [L]
### LANGUAGE REDIRECT RULES end
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ ./index.php?symphony-page=$1&%{QUERY_STRING} [L]
</IfModule>
However this results in none of my pages being found.
FYI A language redirect adds country-code parameters to the url on load.
How do I get these rules to play along?
You're probably missing the query string:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [QSA,L,R=301]
You can also use the QSA flag in your other rules, e.g.:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ ./index.php?symphony-page=$1 [L,QSA,B]
I also added B because you're using the backreference in a query string, so it needs to be escaped.
See the documentation.
Related
I'm using Apache 2.2 with two servers:
Development: localhost/project/public
Production: www.example.com
I have an existing rewrite rule for clean urls (to remove 'index.php' from the url).
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I have one special route that only works if a trailing slash is present:
special route (development): localhost/public/documentation/
special route (production): www.example.com/documentation/
How do I add a rewrite rule to my existing .htaccess to always add a trailing slash, but only for the documentation route?
You just need to add another RewriteRule that matches only against /documentation i.e. without a trailing slash. The first rule adds the trailing slash and then your existing rule adds the index.php.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(documentation)$ $1/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If you would like the browser's address bar to also reflect the trailing slash change the rule to
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^(documentation)$ $1/ [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Don't forget to add these directives first once per .htaccess file:
Options +FollowSymlinks
RewriteEngine on
And then you can try this:
RewriteRule ^/?(documentation)$ /$1/ [R,NC,L]
Or this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} (.+)/([^/]+)$
RewriteRule ^/?(documentation)/(.+)$ /$1/$2/ [R,NC,L]
I'm confused about your question..
I have the following url. I want to rewrite the following url to omit the "view" from the url.
Here is currently my existing mod rewrite
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
http://example.com/page/view/helloworld
How do i rewrite the url to make it
http://example.com/page/helloworld
the keyword helloworld is dynamic.
Expert advice appreciated.
You might want to add the R=301 into the flags if you are worried about transfering SEO.
RewriteEngine On
RewriteRule ^(.+)/view/(.+)$ /$1/$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
The modrewrite below works when i have this which is exactly how i want: "www.mysite.com/files/person/john.html"
files.php?q=person
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*) /$1.php?q=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /files.php?q=$1 [L]
AddHandler application/x-httpd-php5 .html .htm .txt .php
The problem I have is when i have pagination and i have more than one person to display like this: "www.mysite.com/person/2" this becomes person.php, however there is no person.php and the modrewrite should only look at this condition below:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /files.php?q=$1 [L]
The thing is this "www.mysite.com/person/2" works when i only have this modrewrite:
Options +FollowSymLinks
RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)/(.*) /$1.php?q=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /files.php?q=$1 [L]
AddHandler application/x-httpd-php5 .html .htm .txt .php
however, the minute i uncomment it, it stops working? what to do, any ideas?
you can use regex for this.
DirectoryIndex index.php
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([a-zA-Z]+)(.html)?$ /$1.php?q=$2 [L]
RewriteRule ^(.*)/([0-9]+)$ /files.php?q=$1&p=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /files.php?q=$1 [L]
</IfModule>
explanation for regex
() : means group and match that
[a-zA-Z] : means only letters lower or upper
.html : means match ".html"
? : means 0 or more times
[0-9] : means only numbers 0 to 9
+ : means at least 1 or more times
Is there a way to exempt any requests to a subdomain in mod_rewrite? Right now, I have a one-page app that's redirecting everything to index.html.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
</IfModule>
But I want to make sure that calls to subdomain.mydomain.com are permanently exempt from being rewritten.
You can use a condition on the subdomain and the - substitution to stop the rewriting. From http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule
There is a special substitution string named '-' which means: NO
substitution! This is useful in providing rewriting rules which only
match URLs but do not substitute anything for them.
Try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
I have the following URL which is correct...
http://www.mydomain.co.uk/cask-beer/wells-and-youngs
but i need it to render this page:
http://www.mydomain.co.uk/cask-beer?catID=4
This is my current .htaccess which serves other pages correctly already. I want to add something to manage this other rule. I had an attempt where you can cask-beer
php_flag magic_quotes_gpc off
AddDefaultCharset UTF-8
Options -Indexes +FollowSymLinks
#
# Setting rewrite rules
#
<IfModule mod_rewrite.c>
RewriteEngine On
# Set next line to your Wolf CMS root - if not in subdir, then just /
RewriteBase /
# Rules to allow install sequence to test for mod_rewrite support
RewriteRule ^install/index.html$ install/index.php?rewrite=1 [L,QSA]
RewriteRule ^install/index.php$ install/index.php?rewrite=1 [L,QSA]
RewriteRule ^install/$ install/index.php?rewrite=1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# Administration URL rewriting.
RewriteRule ^admin(.*)$ admin/index.php?$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# Main URL rewriting.
RewriteRule ^(.*)$ index.php?WOLFPAGE=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?cask-beer?$1
</IfModule>
Any help would be appreciated.
Cheers
Just add this rule at the top of your other rules:
RewriteRule ^cask-beer/wells-and-youngs$ cask-beer?catID=4 [L,QSA]