RewriteRule not executing properly - .htaccess

I have the following directory structure:
public_html/sites/site_a/
public_html/sites/site_b/
I am trying to write a .htaccess for site_a that rewrites urls. I have the following sitting in the root directory of site_a:
Options +FollowSymLinks
RewriteEngine On
RewriteRule \?page=([a-z]*) $1 [L,QSA]
Basically I would like to have my urls:
http://hostname/sites/site_a/?page=products
Show as:
http://hostname/sites/site_a/products
However this doesn't seem to work. The page shows with the full url.

Try:
RewriteEngine On
RewriteBase /sites/site_a/
RewriteCond %{THE_REQUEST} \ /+sites/site_a/\?page=([^&\ ]+)
RewriteRule ^ %1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ ?page=$1 [L,QSA]
in the htaccess file in your site_a directory.

Related

Remove the .html extension in the URL, when the folder has the same name as the .html file

I have a problem with removing .html extensions from the website address. Everything works fine until I create a folder with the name that some file already has in the given location. For example, the situation with such files:
index.html
example1.html
example1/example2.html
When I try to go from index.html to example1.html, the page does not read the extension and shows me the folder tree instead of the page. It just goes to the example1 folder instead of example1.html. Anyone can help me? I've tried most (if not all) solutions to this problem with Google and nothing works :( I leave the content of .htaccess below. Thanks in advance
SetEnv PHP_VER 5_3
SetEnv REGISTER_GLOBALS 0
RewriteEngine on
# REMOVE .HTML
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
Okay, I found the answer to this question, just add these lines of code to .htaccess.
DirectorySlash Off
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(php|html)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*) /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*) /$1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ^(.*) %{REQUEST_URI}/ [L,R=302]

htaccess query string to path not working

I have this: http://example.com/TEST/user/ticket.php?id=1
I want this: http://example.com/TEST/user/ticket/1
This is my .htaccess file:
Options -Multiviews
RewriteEngine On
RewriteBase /TEST/user/
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?ticket/(.*?)/?$ /ticket.php?id=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /ticket\.php\?id=([^\&\ ]+)
RewriteRule ^/?ticket\.php$ /ticket/%1? [L,R=301]
What's wrong?
.php extension is correctly hidden, but query string instead is not "converted" to path. It gives me:
500 internal server error
The server logs should show you the reason for the error.
One problem here is that in the last line you use a pattern that matches just ticket.php and not ticket.php?id=
RewriteRule ^/?ticket\.php$ /ticket/%1? [L,R=301]
Try
RewriteRule ^/?ticket\.php?id=([^\&\ ]+).*$ /ticket/$1? [L,R=301]
The RewriteCond shouldn't be necessary.

UrL Rewrite using code with my domain

URL : http://domainname.com/index.php?p=top-games
I need to rewrite this as http://domainname.com/top-games
How I do this using htaccess file? Please can any one give me the htaccess code.
Thanks
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
</IfModule
Create .htaccess file in your root folder and paste code
You're probably looking for redirect in reverse direction.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /index.php?q=$1 [L,QSA]

.htaccess doesn't work

I have the following .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mykeyword$ news.php [L,QSA,NC]
However, when I open the news.php, the url is still the same, that is www.mydomain.com/news.php instead of www.mydomain.com/mykeyword
I make the following test:
RewriteEngine on
RewriteRule ^test\.html$ test.php [L]
I upload 2 files on my server, test.html and test.php and after I type www.mydomain.com/test.html, my php page was displayed, so that mean that I have no problem with my settings. What on earth I am doing wrong???
Any help will be deeply appreciated.
Regards,Zoran
Change your .htaccess to this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(mydomain\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+news\.php [NC]
RewriteRule ^ mykeyword [R=301,L]
RewriteRule ^mykeyword/?$ news.php [L,NC]
The rewrite rule translates from the URL supplied by the user to the URL seen by the server. Try browsing to www.mydomain.com/mykeyword - you should see the page news.php.

%2520 (Double space) in URL for URL with spaces

My URL structure is like
http://www.example.com/folder/index.php?dir=dir1
To be able to access it from
http://www.example.com/folder/dir1
and at the same time redirect the 1st URL to 2nd one, my htaccess (in 'folder') is
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]
The trouble is that if any directory name in URL contains a 'space', the URL shows %2520 instead of the 'space'.Please advice in modifying the htaccess so it shows %20 or preferrably a simple 'space'?
try adding a NE on the redirect ie
RewriteRule ^ %1? [L,R=301,NE]
EDIT
Since you've read my htaccess, do you see any possiblity of shortening it further
Below are a couple of comments
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]
#this looks redundant with last rule, and could be deleted?
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
#this says if not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
#and this says if it IS an existing directory
#Is this what you wanted, or should it be not an existing directory i.e
# RewriteCond %{REQUEST_FILENAME} !-d instead
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]

Resources