What would be the rule to redirect page depending the parameter ?
For example:
I want : domain.com/en redirect to domain.com?lang=en.
Thanks for your help.
You can use this rule:
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)/?$ ?lang=$1 [L,QSA]
Related
I want to redirect all non existing directories to index.php.
This URLs should be redirected (examples):
/pages/page1/
/pages/page1
URLs not to be redirected because they are existing (examples):
/admin/
/admin
Any ideas?
Thanks!
You can use this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^.]+$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d is the condition to check if it is not a directory.
^[^.]+$ is regex pattern to ensure there is no extension in request URI.
Try it like this,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [R=301]
So the problem I'm looking at and haven't managed to solve!
For example the url is http://someurl.com/brands/brand
What I want to accomplish is that htaccess lets te request go to url: http://someurl.com/brands/brand but removes the "brands" part from the url.
Also if the url: http://someurl.com/brands is called the page "brands" needs to be displayed. So only when there is a brand after /brands/ it needs to do a URL rewrite to http://someurl.com/brand
I have tried this but this piece does a redirect to the new location witch doesn't exist.
RewriteRule ^onze-merken/(.*)$ /$1 [L,R=301]
So I need the above with out the redirect, it only needs to rewrite the URL.
Any help would be greatly appreciated, Thanks!
This is now my htaccess part where the rewriting is done!
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /onze-merken/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# Only rewrite if the directory doesn't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# internal forward from pretty URL to actual one
RewriteRule ^((?!onze-merken/).+)$ /onze-merken/$1 [L,NC]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule ^(uploads/([a-z0-9_\-\.]+)/__images/custom/(.*)/)(.*)$ http://someurl.com/uploads/$2/__images/custom/$3/$4 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /brands/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!brands/).+)$ brands/$1 [L,NC]
I have an hidden folder hidden_folder which can be accessed by the server with
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1
Now, I want to redirect all unavailable pages to 404 error page content.
Illustration 1 : When user tries to go here http://website.com/some_unavailable_uri, the url of the page doesn't change but the content will be the one of /error/404.php
Illustration 2 : http://www.apple.com/test. This page does not exist, the url does not change, but the content displays error.
Basically, it should be if file does not exist, then redirect content. I'd tried something but with no success.
First try :
RewriteEngine On
RewriteCond hidden_folder%{REQUEST_URI} -f # %{REQUEST_URI} already has "/" at its beginning
RewriteRule ^.*$ error/404.php [NC,L]
Second try :
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} -f
RewriteRule ^.*$ error/404.php [NC,L]
Any idea ?
Looks like you just need a simple ErrorDocument with use of http:// in target since that causes URL to change in the browser.
Use this line top of your .htaccess:
ErrorDocument 404 /error/404.php
And don't forget to comment out your rewrite rules doing same.
The conditions are tested also on redirects, so you must make sure that the 404 redirect doesn't catch more than it should.
What this means is that %{REQUEST_URI} will contain hidden_folder once it's been redirected, so you must add a RewriteCond to avoid it.
This should work:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/hidden_folder%{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !^/hidden_folder
RewriteRule ^(.*)$ error/404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1 [L]
Thank you Outlyer.
The actual working code is :
RewriteEngine On
# If the requested file is not in hidden_folder as a file
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} !-f
# AND
# If the requested file not is in hidden_folder as a folder
RewriteCond %{DOCUMENT_ROOT}hidden_folder%{REQUEST_URI} !-d
# AND
# If the requested file is not in root as a file
RewriteCond %{REQUEST_FILENAME} !-f
# AND
# If the requested file is not in root as a folder
RewriteCond %{REQUEST_FILENAME} !-d
# AND
# ???
RewriteCond %{REQUEST_URI} !^/hidden_folder
# THEN Redirect content
RewriteRule ^(.*)$ error/404.php [L]
####################################################
# Enable the deletion of hidden_folder in urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ hidden_folder/$1 [L]
I have URL rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [NC,L]
When this URL is called: http://example.com/suyash
It internally calls http://example.com/sites/suyash but keeps the URL as http://example.com/suyash only. Please note that the folder sites/suyash actually exists
The issue I am facing is that when I call a URL: http://example.com/Suyash (Uppercase) it shows a 404 error.
When I use the mod_speling module and add the following to the code:
CheckSpelling On
and now when I call http://example.com/Suyash it changes the URL to http://example.com/sites/suyash
How do I get it to only change URL from http://example.com/Suyash to http://example.com/suyash?
mod_speling will redirect the browser in order to fix spelling, there's no way to avoid this. The only thing I can think of is to try proxying the request internally by using the P flag. You'll need to make sure you have mod_proxy loaded. I haven't tested this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [NC,L,P]
I would say keep CheckSpelling off.
And then have your rule like this:
CheckSpelling Off
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$0 -f [NC]
RewriteCond %{DOCUMENT_ROOT}/$0 -d [NC]
RewriteCond %{REQUEST_URI} !^/sites/ [NC]
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [L]
I have a very simple rewrite rule. I would like the second parameter in the URL to be optional, but as it stands at the moment i have to pass it in the URL:
RewriteEngine On
RewriteRule ^signup/([^/]*)/([^/]*)/$ /signup/index.php?e=$1&s=$2 [L]
I have the URL /signup/PARAMETER/OPTIONAL-PARAMETER/
How can I make the url work if I leave the second parameter off?
Joe
Just add a single parameter rewrite after it:
RewriteEngine On
RewriteRule ^signup/([^/]*)/([^/]*)/$ /signup/index.php?e=$1&s=$2 [L]
# single parameter
RewriteRule ^signup/([^/]*)/$ /signup/index.php?e=$1 [L]
I used the answer from this question htacess and two post parameters .... Thanks aeshabana for the tip!
I also needed to add a slash to the URLS so here is my final .htaccess
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule ^signup/(.+)/$ /signup/index.php?uri=$1 [QSA,L]