Ive started out with the most basic rewrite there is, take any request and give it to my index page:
RewriteEngine On
RewriteRule ^.*$ index.php [L,QSA]
Im trying to change it now so lets say i have a directory called special, I actually want to be able to go to http://example.com/special and access whatever files are in there.
I tried this:
RewriteRule ^special/.*$ index.php [L, QSA]
but it didnt work.
Try either this:
RewriteRule !^special/ index.php [L]
Or this:
RewriteRule ^special/ - [L]
RewriteRule ^ index.php [L]
If you want this to apply to any directory, try this:
RewriteEngine On
RewriteCond %{REQUESTFILENAME}!-d
RewriteRule ^ index.php [L]
Related
I'm on Apache2 and in directory "/demo". Inside this directory there is:
v1/index.html
v2/index.html
I want to redirect like this:
"/demo", "/demo/" and "/demo/*" => v1/index.html
"/demo/static", "/demo/static/" and "/demo/static/*" => v2/index.html
I tried this, but it didn't work:
RewriteCond %{REQUEST_URI} !static [NC]
RewriteRule .* v1/index.html [L]
RewriteCond %{REQUEST_URI} static [NC]
RewriteRule .* v2/index.html [L]
Anyone with an idea how to make this work?
Additional details:
Root directory is here: /var/www/dev and contains my directory demo. .htaccess is in inside the directory demo.
What about:
RewriteEngine On
RewriteRule .*demo/static.* v2/index.html [L]
RewriteRule .*demo.* v1/index.html
Since I don't know what is the document root here, I used .*, so it can catch more than you want, but this can be a starting point.
Maybe that's a solution?
RewriteRule ^static/?$ v2/index.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ v1/index.html
I want to change:
domain/link to domain/index.php?q=link
and
domain/notice/20151212/title-with-a to domain/index.php?q=notice&date=20151212&title=Title-with-a
with this .htaccess file:
RewriteEngine On
RewriteRule ^([a-zA-Z-]+)$ index.php?q=$1 [L]
RewriteRule ^notice/([0-9]+)/([A-Za-z0-9_-]+)$ index.php?q=notice&date=$1&title=$2 [L]
If i enter into domain/link or domain/link2, goes well but if i try to enter to domain/notice/20151212/title-with-a, I can't because it cant get CSS, image, JS files (tries to get domain/notice/20151212/js/theme.js when the file is in domain/js/theme.js) but i can enter without problems by the URL:
domain/index.php?q=notice&date=20151212&title=Title-with-a
What am I doing wrong?
I check this regex on http://htaccess.mwl.be/ and i think the htaccess file is right.
Thank you.
try to do this
RewriteEngine On
RewriteRule ^notice/([^/]+)/([^/]+)/$ index.php?q=notice&date=$1&title=$2 [L]
RewriteRule ^([a-zA-Z-]+)$ index.php?q=$1 [L]
and try to revert order like here
RewriteEngine On
RewriteRule ^notice/([0-9]+)/([A-Za-z0-9_-]+)$ index.php?q=notice&date=$1&title=$2 [L]
RewriteRule ^([a-zA-Z-]+)$ index.php?q=$1 [L]
EDIT
add [L] at the end of second line and try my url, it works
http://lab.maurosala.it/notice/a/b/
if you don't want the / at the end of url use this
RewriteRule ^notice/([^/]+)/([^/]+) notice.php?q=notice&date=$1&title=$2 [L]
I'm trying to set the first rule as optional, so both of these URLs will work:
/username
/username/history
/history
The username will never clash.
RewriteRule ^([a-z]+)/((history|analysis|messages|photos)?)$ pages/dashboard.php?username=$1&page=$2 [L]
With the above I have /username/history working. Cannot figure howto get the others.
EDIT: The above snippet is the result of trying to merge these three lines into one.
RewriteRule ^(history|analysis|messages|photos)?$ pages/dashboard.php?page=$1 [L]
RewriteRule ^([a-z]+)/(history|analysis|messages|photos)?$ pages/dashboard.php?username=$1&page=$2 [L]
RewriteRule ^([a-z]+)$ pages/dashboard.php?username=$1 [L]
You can use this rule:
RewriteRule ^(history|analysis|messages|photos)/?$ pages/dashboard.php?page=$1 [L,NC,QSA]
RewriteRule ^([a-z]+)(?:/(history|analysis|messages|photos))?/?$ pages/dashboard.php?username=$1&page=$2 [L,NC,QSA]
I'am struggling with this for a couple of days.
Folder structure is:
index/files/pages/
In main folder (index) there is index.php - this is the only index.php file in whole page.
In files folder there are php files that are actually pages of the website.
In files folder there are couple of more folders (pages) that hold different pages - I just grouped the php fiels into folders to keep it tidy.
I am trying to achivie this.
When I enter url like index/files/pages/somename.php - I want to get 404 or whatever
When I enter url like index/files/somename - this gets rewritten to index/files/pages/somename.php, but the address looks clean.
Now whatever I do I can't get both rules to work, either one overwrites another or nothing works, I get 500 all the time.
Here is my rule that actually works:
RewriteRule ^([-0-9a-z]+)$ pages/$1.php - this is the scenario 2 and its ok.
Now when I add rule that handles direct access to php files, the previous rule is also affected and gets overwritten and everything throws 404, 500 etc. Flags like [L] etc don't have any effect, changing the order also does nothing.
Also I want to forbid direct access to index.php and redirect to index folder (and show clean url)
Here are some additional rules i tried, but no result. As I said earlier setting flags give no results.
RewriteCond %{REQUEST_URI} ^.*\/pages\/.*\.php$
RewriteRule ^(.+)$ [R=404,L]
RewriteRule ^([-0-9a-z]+)$ pages/$1.php
RewriteCond %{REQUEST_URI} ^(.+)\/index\.php
RewriteRule ^(.*)$ index/ [R=301,L]
RewriteCond %{REQUEST_URI} ^\/(.*)\.php
RewriteRule ^(.*)$ [R=404,L]
Thankx and regards
Try these rules:
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ index/ [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} \.php[\s?/] [NC]
RewriteRule ^ [F]
RewriteRule ^index/?$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
I need to do a very simple URL rewrite but my RewriteRule is not working.
I want this URL: http://myweb.com:8080/MySite/bla?bla
To become this: http://myweb.com:8080/MySite/index.php
My .htaccess file content is like this:
RewriteEngine On
RewriteRule bla\?bla index.php
It is located in "MySite" folder. I can do other url-rewriting rules with success but I got stuck whenever I need to write a rule with question mark inside.
What am I doing wrong?
You need to use the %{QUERY_STRING} rewrite condition for this.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^bla$
RewriteRule ^/?bla$ index.php [NC,L]
Please, note that the ? in the rewrite rule is not there to match against the ? in the query string. That part is handled completely by %{QUERY_STRING}. The [NC] just makes the rule case-insensitive and [L] marks the rule as last.