I have all of my customers sites in a directory on my subdomain:
customers.example.com/sites/customer_name
I want to rewrite the url so my customers only need to write
customers.example.com/customer_name
I have tried some different htacces scripts but none of them works. And one of them gives me a error 500 internal server error, so my mod_rewrite is active
Here is my current .htaccess file:
RewriteEngine On
RewriteRule ^sites/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
RewriteRule ^(.*)$ /sites/$1/ [QSA,L]
Since the rewrite engine loops, this pattern ^(.*)$ will blindly match everything, including your rule's target: /sites/something. It'll continue to append /sites/ to the front of the URI until you end up with something like /sites/sites/sites/sites/sites/sites/sites/ etc.
You need to add some conditions to prevent the looping, something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sites/$1/ [L]
or
RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^(.*)$ /sites/$1/ [L]
or
RewriteCond %{DOCUMENT_ROOT}/sites%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sites%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sites/$1/ [L]
This should work… If understand your end goal correctly. But unclear if you want the content accessible from customers.example.com/sites/customer_name and customers.example.com/customer_name at the same time:
RewriteEngine on
RewriteRule ^/sites/(.*)$ http://customers.example.com/$1 [L,R=301]
Related
I read tons of topics about this here on SO, but for some reason it not works for me, and I am totally confused. I know, I mess up something, I just cant figure it what.
I have a site, and the index.php is calling the router, and show the content that I want.
I've created an admin' page, and I want the webserver to use the /admin/index.php for every request what is starting with /admin/
Here is what I try:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/(.*)$ admin/index.php?action=$2 [L,NC]
RewriteRule ^(.*)$ index.php?action=$1 [L]
I echoing some information from both index.php to see which handles my request.
If I am writing http://localhost it says Frontend.
When I try http://localhost/admin/ it says Frontend again.
If I am moving the admin' line below the first rule like this:
RewriteRule ^(.*)$ index.php?action=$1 [L]
RewriteRule ^admin/(.*)$ admin/index.php?action=$2 [L,NC]
then http://localhost/admin/ says Admin what is actually good!
But if I try http://localhost/admin/users what is actually not there, because the admin's router should handle that, it says Frontend.
How should I set .htaccess to use /admin/index.php in all cases, where a http://localhost/admin/ in the URI?
Have it like this:
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^admin/(.*)$ admin/index.php?action=$1 [L,NC,QSA]
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]
I'm rewriting my portfolio website urls by using the following code in a htaccess file,
RewriteEngine On
RewriteBase /
#remove enter code here.php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
It all works until I want to navigate to another page from the blog-post.php page
for example, let say I want to go to the about page the url becomes cgarcia.design/web/about and it should be cgarcia.design/about so the page can load properly. Now what would I need to change in the htaccess file to accommodate files within folders?
my nav structure is the following
work
about
resume
blog - posts folder - entry
contact
Thank you for any suggestions.
Add this to the ned of your document:
RewriteRule ^$ web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ web/$1
I am writing a website which is dynamically populated through an Oracle database.
I have completed the desktop site and am now required to create a mobile site. Due to how different the sites are planned to look, I have opted to create 2 different "template" like websites for the mobile and desktop sites.
However, for my desktop site, everything is built off the index.php file in order to allow it to be completely dynamic. Pages are therefore look like www.domain.com/index.php/page in the url.
For the desktop site, this works. I am using a generic index.php removal rewrite rule in order to then make the url www.domain.com/page however still display the same page as the previous URL.
My issue, is that now I have a www.domain.com/mobile/index.php. Which has been created and for the most part has been working, however when trying to add addition dynamic pages to the mobile site. www.domain.com/mobile/index.php/about for example just redirects to www.domain.com/mobile/ and it doesn't even include the about part of the URL.
After much debugging, I have discovered it is definitely the .htaccess that is causing the issue.
If you have any insight into my issue, please help me out.
Thanks in advance
EDIT 1
Rewrite Rules are as follows
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
You can use this code in your /mobile/.htaccess file:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /mobile/
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(?:/(.*))?$ $1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
This will override all the rules present in parent .htaccess for /mobile/ URI path.
Simplified version to make it work in root .htaccess:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(mobile)/(.*)$ $1/index.php/$2 [L,NC]
# Directs all EE web requests through the site index file
RewriteRule ^(.*)$ /index.php/$1 [L]
Based on the debugging you mentioned, there is a rule in your .htaccess which is rewriting www.domain.com/mobile/index.php/about to www.domain.com/mobile/. So, if you find which rule this is, you can add one above it that will catch requested URLs for your mobile pages and then not allow the problematic following rule to run. Something like this:
RewriteRule ^mobile/index.php/([a-zA-Z0-9-_]+)$ ^mobile/index.php/$1 [L,QSA]
The L ensures that if the user's request matches this rule, no further rules (including the one causing the issue) will be executed.
Thank you for the answers you've both given, however neither of them worked, I've now solved the issue, it was to do with the final RewriteRule at the end
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I needed to change it to
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/mobile/.* [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /mobile/.* [NC]
RewriteRule ^(.*)$ mobile/index.php/$1 [L]
So that it would work with both mobile and desktop sites.
This is driving me mad. I'm trying to use .htaccess to redirect a subfolder (that doesn't exist) to the index page, using the subfolder name as the variable. ie:
http://www.website.com/john/
redirects to:
http://www.website.com/index.php?name=john
I've tried this (and various others) with no luck:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?name=$1
Here is an example, how you can do this:
# turn mod_rewrite engine on
RewriteEngine On
# rewrite a physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/layout/" [OR]
RewriteCond %{REQUEST_URI} "/javascript/"
# rewrite rules
RewriteRule .* - [L]
RewriteRule (.*) index.php?_route=$1 [QSA]
This one also denies access to folders you don't want to have public.
Try this one:
RewriteRule ^([^/]*)/$ /?name=$1 [L]
RewriteRule ^([^.]*)$ /index.php?name=$1 [L,QSA]
I'm trying to rewrite URLs as follows:
www.example.com/module-name(/) => handler.php?module=module-name
www.example.com/module-name/list/of/arguments(/) => handler.php?module=module-name&args=list/of/arguments (which I explode later in the script)
If module-name is missing or not allowed, it loads the home page of the website. No problems here.The problem is that mod_rewrite isn't working as it should. I wrote these rewrite rules...
RewriteEngine On
RewriteRule ^([^/]*)/?$ handler.php?module=$1
RewriteRule ^([^/]+)/(.+)/?$ handler.php?module=$1&args=$2
...but instead of passing module-name as module to the handler, the rewrite engine passes the name of the script itself, handler.php. I tried with different regex in the last two days, but the result is always the same. I don't know what to do anymore!
The rewrite rules are inside an .htaccess, placed in the document root (together with handler.php), and I'm running xampp on an Ubuntu 11.10 64-bit machine.
Thanks in advance!
Yep, after rewrite occurs, it goes to next cycle instead of existing immediately as you would expect.
You need to alter your rule (or add separate condition) to ignore requests to handler.php file. For example:
RewriteRule ^(?!handler\.php)([^/]*)/?$ handler.php?module=$1 [L]
RewriteRule ^(?!handler\.php)([^/]+)/(.+)/?$ handler.php?module=$1&args=$2 [L]
or with extra separate condition:
RewriteCond %{REQUEST_URI} !/handler\.php$
RewriteRule ^([^/]*)/?$ handler.php?module=$1 [L]
RewriteCond %{REQUEST_URI} !/handler\.php$
RewriteRule ^([^/]+)/(.+)/?$ handler.php?module=$1&args=$2 [L]
or even like that:
# do not touch requests to handler.php
RewriteRule ^handler\.php$ - [L]
# our rewrite rules
RewriteRule ^([^/]*)/?$ handler.php?module=$1 [L]
RewriteRule ^([^/]+)/(.+)/?$ handler.php?module=$1&args=$2 [L]
You can also go even this way (it really depends on your rewriting logic)
# do not do anything for requests to existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# our rewrite rules
RewriteRule ^([^/]*)/?$ handler.php?module=$1 [L]
RewriteRule ^([^/]+)/(.+)/?$ handler.php?module=$1&args=$2 [L]
A problem could be that existing file-names get rewritten as well. To avoid that, you can add:
RewriteCond %{REQUEST_FILENAME} !-f # ignore existing files
RewriteCond %{REQUEST_FILENAME} !-d # ignore existing directories
So for you it would be:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f # ignore existing files
RewriteCond %{REQUEST_FILENAME} !-d # ignore existing directories
RewriteRule ^([^/]*)/?$ handler.php?module=$1
RewriteCond %{REQUEST_FILENAME} !-f # ignore existing files
RewriteCond %{REQUEST_FILENAME} !-d # ignore existing directories
RewriteRule ^([^/]+)/(.+)/?$ handler.php?module=$1&args=$2