My htaccess rewrite doesnt work - apache

I have a folder structure like this
/localhost/parent/
I have an .htaccess residing in
/localhost/parent/
I want to rewrite everything from /localhost/parent/WebContent/ to /localhost/parent/
here is my code
RewriteEngine on
RewriteRule ^$ WebContent/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^WebContent/
RewriteRule ^(.*)$ WebContent/$1
For some reason the first two lines work fine, but the rest doesn't work. So I am able to redirect only homepage.
Update
RewriteBase /parent/
RewriteEngine on
RewriteRule ^$ WebContent/$1
#RewriteCond %{REQUEST_URI} !WebContent
RewriteRule (.*) /parent/WebContent/$1 [R=301,L,QSA]
Goes into an infinite loop. I just need to avoid that loop now

Try switching the order of the lines so that
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
comes before everything else.
You could also try adding this line:
RewriteBase /WebContent/

I used this
RewriteEngine on
RewriteRule ^$ /parent/WebContent/$1
RewriteCond %{REQUEST_URI} !WebContent
RewriteRule ^(.*)$ /parent/WebContent/$1

Related

url rewriting synthax with .htaccess

I am on apache 2 and I would know if my htacces is correct
my url are for example :
localhost/test/boutique/index.php
localhost/test/boutique/index.php/language,en
localhost/test/boutique/index.php/Products/Description/products_id,1
localhost/test/boutique/index.php/Products/Description/products_id,2/language,fr
What is the best approach for a good url like above
In suppose index.php must deseapear to hav someting like that
localhost/test/boutique/Products/Description/products_id,1
I try this but it does'nt work
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://localhost/test/boutique/index.php/$1 [PT,L]
Assuming /test/boutique/ is a real directory, you can use these rules inside /test/boutique/.htaccess:
RewriteEngine On
RewriteBase /test/boutique/
# remove index.php if entered directly by clients
RewriteCond %{THE_REQUEST} /index\.php/(\S*)\s [NC]
RewriteRule ^ %1 [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!index\.php$).* index.php/$0 [L,NC]

.htaccess returns 500 but gets fixed when re-arranging the rules

Some weird stuff happening in .htaccess file
File that does not works
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/([a-z0-9_-]+)$ $1/post?id=$2 [L,QSA]
RewriteRule ^(.*?)/edit/([a-z0-9_-]+)$ $1/editpost.php?id=$2 [L,QSA]
#RewriteRule ^users/(\d+)*$ ./profile.php?id=$1
But this works
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/edit/([a-z0-9_-]+)$ $1/editpost.php?id=$2 [L,QSA]
RewriteRule ^(.*?)/([a-z0-9_-]+)$ $1/post?id=$2 [L,QSA]
#RewriteRule ^users/(\d+)*$ ./profile.php?id=$1
I simply re-arranged the rewrite rule and it fixed the issue. Can anyone explain why this happened? I was stuck in this for more than 2 hrs.
I am new to URL rewriting so please explain in detail. And what actually [L, QSA] does?
RewriteCond is only applicable to nearest RewriteRule. So in your first example it is applied to only this rule:
RewriteRule ^(.*?)/([a-z0-9_-]+)$ $1/post?id=$2 [L,QSA]
Two bottom RewriteRule are executed without any RewriteCond and cause infinite loop.
You can have your rules as:
RewriteEngine On
RewriteBase /
# skip all files and directories from rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(.*?)/edit/([\w-]+)/?$ $1/editpost.php?id=$2 [L,QSA]
RewriteRule ^users/(\d+)/?$ profile.php?id=$1 [L,QSA]
RewriteRule ^(.*?)/([\w-]+)/?$ $1/post?id=$2 [L,QSA]

Rewrite urls in htaccess file - remove query string

I have never edited the .htaccess file before so sorry for my awful attempt
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ http://example.site/index/%1 [R=302,L]
I am tryting to rewrite http://ex.com/shop/?s=sa to look like http://ex.com/shop/sa
I am assuming you are using rewriterules in shop directory and index.php is handling the data then try with below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)$ index.php?s=$1 [L]

multiple RewriteRules in htaccess file, the first one over writes the second one?

I am trying to run multiple RewriteRules in my htaccess file.
however, the first RewriteRule, overwrites the second RewriteRule for some reason!
this is what i have in my htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^([a-zA-Z0-9-/]+).html$ items.php?itemsurl=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ items.php?itemsurl=$1 [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^([a-zA-Z0-9-/]+).html$ blog.php?blogurl=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ blog.php?blogurl=$1 [L]
the first RewriteRule works fine but if I click on the links for blog.php?blogurl= it will simply take me to the items.php!
I could put the RewriteRules for the blog.php at the top of the htaccess file and put the RewriteRules for items.php bellow it and it will make the blog.php rewriterule work for blog.php but it will make the rewriterules for the items.php stop working and everything will point to the blog.php page!
So basically, only the first RewriteRule in the htaccess file works and it will overwrite the second one somehow.
could someone advise on this please?
Replace your code with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/]+)\.html\/?$ items.php?itemsurl=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/]+)\.html\/?$ blog.php?blogurl=$1 [L]
RewriteCond affects only to the first RewriteRule.
The apache variables should be in these brakes: {}, not these ()
And one more thing - to check . you should escape it so: \.
As discussed in comments, you can use following rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^items/([\w-]+)\.html\/?$ items.php?itemsurl=$1 [L,QSA,NC]
RewriteRule ^blog/([\w-]+)\.html\/?$ blog.php?itemsurl=$1 [L,QSA,NC]

Add directory in the middle of a URL with hataccess

My current .htacces file looks like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
What I am trying to accomplish (as well as the above) is to change a url such as http://domain.com/pages/pagename to something like http://domain.com/index.php/pages/view/pagename.
Keeping in mind that I still require urls without the /page/ part such as http://domain.com/search to go to http://domain.com/index.php/search. I am working with CodeIgniter.
What I have come up with so far is:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/pages/(.*)
RewriteRule ^(.*)$ ./index.php/pages/index/$1 [L,QSA]
But it isn't working.
Try this one:
RewriteEngine on
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# /pages/ specific rule
RewriteRule ^pages/(.*)$ index.php/pages/index/$1 [L]
# everything else
RewriteRule ^(.*)$ index.php/$1 [L]
The above assumes that http://domain.com/pages/ is not a real folder.
RewriteRule ^pages/(.*) /index.php/pages/index/$1