Apache mod rewrite like twitter url - php

Here is my .htaccess code
ErrorDocument 404 /404.php
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ profile?username=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php
RewriteRule ^s/([^/]*)$ /s.php?share=$1 [L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I would users on my website to be able to access their profile like how twitter does. For instance www.mywebsite.com/Jim would actually be rewritten as www.mywebsite.com/profile?username=Jim.
Currently the only part of the code that works is allowing url without the php extension. For instance www.mywebsite.com/home instead of www.mywebsite.com/home.php.
Moreover, the rewrite rule for s.php does not work like it should. I would like the page www.mywebsite.com/s/123456 to be written as www.mywebsite.com/s?share=12345.
I don't if the order of my htaccess is wrong but none of these seem to work. Additionally when a page is not found I'm still getting the default Apache 404 even though I have a rewrite rule to 404.php

Your redirect needs to be first, then your profile, then your share, then the most generalized php extension:
ErrorDocument 404 /404.php
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ profile.php?username=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^s/([^/]*)$ /s.php?share=$1 [L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]

Related

htaccess redirect domain with query string to subdomain with excludes

We have moved our site to a subdomain while on the main domain we have a new site with new url's. What we are trying to do is having the old url's redirect to the subdomain while the excluding the new url's.
here's examples of the old url's:
http://www.domain.co.il/index.php?dir=app_admin&page=ip_stat&op=list&pos=0
should be redirected to:
http://sub.domain.co.il/index.php?dir=app_admin&page=ip_stat&op=list&pos=0
While the new site url's (do not contain php) and the homepage should not be effected.
This is what our htaccess looks like:
It does redirect the url's to the sub, but does not exclude the new pages and redirect them to the subdomain home (sub.domain.com)
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !contact(|$)
RewriteCond %{REQUEST_URI} !about(|$)
RewriteCond %{REQUEST_URI} index\.php
RewriteRule ^(.*)$ http://sub.domain.com [R=301,L]
Hopefully someone can help us get this right, we get many 404 in our gwt now :/
Thanks for any help !
Fix some regex and reorder your rules:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} !/(contact|about) [NC]
RewriteCond %{THE_REQUEST} !\.(jpe?g|gif|bmp|png|tiff|css|js) [NC]
RewriteRule ^(.+)$ http://sub.domain.com/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Also test this in a new browser to avoid old browser cache.
Here's the full code for the site.
The way things are set up is that the new site is being called from a folder named "main" while the old site is being called from the root directory.
So we actually have 2 htaccess.
Here's the root htaccess code:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^/([^.]+)\.php
RewriteRule ^(.*)$ http://sub.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} www.domain.com$
RewriteCond %{REQUEST_URI} !^/main
RewriteRule ^(.*)$ /main/$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule ^admin$ index.php?dir=app_misc&page=login [L]
RewriteRule ^admin/$ index.php?dir=app_misc&page=login [L]
RewriteRule ^sitemap.xml$ sitemap.php [L]
RewriteRule ^robots.txt$ robots.php [L]
RewriteRule ^adv$ adv/index.php [L]
RewriteRule ^adv/$ adv/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(([0-9]+)_([^/]+)|([0-9]+))$
RewriteRule (.*) /404.php?a=%{REQUEST_URI} [L]
and the htaccess in the "main" folder:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !(contact|about) [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
I've tested on few browsers and in incognito mode as well
Thanks
Instead of excluding urls which caused necessary css & js files to redirect as well, I solved it by creating a redirect that effect only url's with php.
RewriteCond %{THE_REQUEST} ([^.]+)\.php [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L,NE]

.htaccess not redirecting home page

Here is my .htaccess on a Linux system:
ErrorDocument 401 ./error/
ErrorDocument 403 ./error/
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.website.co.uk/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.website.co.uk/$1 [R=301,L]
Everything works as it should; www is added to non-www requests and a trailing slash is added. However when visiting www.website.com (which is added as a parked domain on cPanel) the user is NOT redirected to www.website.co.uk
If the visit website.com (note no www) then they ARE redirected.
What do I need to add/change in .htaccess?
Have your rules like this:
ErrorDocument 401 ./error/
ErrorDocument 403 ./error/
RewriteEngine on
RewriteBase /
# if not www.website.co.uk then redirect to it
RewriteCond %{HTTP_HOST} !^www\.website\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.website.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://www.website.co.uk/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]

use htaccess to make the rewrite url

hello all i am having a project where i need to convert this url www.domain.com/page.php?brand=abcd to www.domain.com/abcd
i know that this is possible with .htaccess file
currently in my htaccess file i have folowing code
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.domain.com [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
ErrorDocument 404 /error404.php
ErrorDocument 403 /error404.php
Options -Indexes
please help me to do this and what line should i add to .htaccess file to achive the purpose
i tried adding
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /page.php?brand=$1 [L,QSA]
Your last rule should work for internal rewrite to /page.php?brand=abcd.
For redirecting /page.php?brand=abcd to /abcd you can use this new rule:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+page\.php\?brand=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /page.php?brand=$1 [L,QSA]
you can Try this
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ http://litelmaxx.com%{REQUEST_URI} [R,L]
Options -Indexes

Mod_rewrite, with exception that refers to another rule

i want to use mod_rewrite on my page and already have some rules
now i want to redirect to artist.php?name=(PARAMETER) except it matches another rule
at the moment i use a "a/" before artist.php because i dont know how to solve this problem
so if i want so see the artist "Alex" ist just have to type domain.com/Alex and if i want to go to the about page i just have to type domain.com/about (just like facebook or twitter)
of course i'm thankfully for each improvement
Here is the .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^cover/([0-9]*)(s|m)?\.jpg$ /cover.php?id=$1&size=$2
RewriteRule ^dl/([^/]*)/([^/]*)$ /download.php?$1=$2
RewriteRule ^track/([^/]*)\.mp3$ /song.php?id=$1
RewriteRule ^my(/)([^/]*)$ /my.php?requested=$2
RewriteRule ^album/(.*)-([0-9]{0,6})$ /album.php?id=$2
RewriteRule ^a/([^/]*)$ /artist.php?name=$1
RewriteRule ^a/([^/]*)/music$ /music.php?name=$1
RewriteRule ^(artists|about|charts|contact|myprofile|editprofile|mymusic|uploadsongs|logout|genres)$ /$1.php
RewriteRule ^t$ /t.php
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com(/)?.*$ [NC]
RewriteRule .*\.(mp3)$ - [F,NC]
ErrorDocument 404 /errors/404.php
p.s.: i'm sorry for my bad english
You need to rearrange some of your rules, and add L flags so they don't accidentally get applied:
RewriteEngine On
RewriteBase /
RewriteRule ^cover/([0-9]*)(s|m)?\.jpg$ /cover.php?id=$1&size=$2 [L]
RewriteRule ^dl/([^/]*)/([^/]*)$ /download.php?$1=$2 [L]
RewriteRule ^track/([^/]*)\.mp3$ /song.php?id=$1 [L]
RewriteRule ^my(/)([^/]*)$ /my.php?requested=$2 [L]
RewriteRule ^album/(.*)-([0-9]{0,6})$ /album.php?id=$2 [L]
RewriteRule ^a/([^/]*)$ /artist.php?name=$1 [L]
RewriteRule ^a/([^/]*)/music$ /music.php?name=$1 [L]
RewriteRule ^(artists|about|charts|contact|myprofile|editprofile|mymusic|uploadsongs|logout|genres|t)$ /$1.php [L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com(/)?.*$ [NC]
RewriteRule .*\.(mp3)$ - [F,NC]
# assume anything else is a username
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /artist.php?name=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/music$ /music.php?name=$1 [L]
ErrorDocument 404 /errors/404.php

Using htaccess and mod_rewrite to display new-page instead of newpage

I'm working on my website and I created a few new web pages that explains more details of my website. Now for seo purposes I know google prefers for the link to contain the key words. So the pages I'm making could be call new shoes. Now I have gotten the pages to work as website.com/newshoes but since google may view this as one word, I'm working on making it display website.com/new-shoes instead using htaccess but I can't get it to work. This is what I came up with but it didn't work.
RewriteRule ^([^/]*)-([^/]*)$ /index.php?$1$2 [QSA,L]
So yeah can anyone help me out here. Thanks!
[edit]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^reflap\.com [NC]
RewriteRule (.*) http://www.reflap.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?$1 [QSA,L]
RewriteRule ^([^/]*)/$ /index.php?$1 [QSA,L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?$1&$2 [QSA,L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?$1&$2 [QSA,L]
RewriteRule ^/facebook-chat/ http://chat.facebook.com/ [P]
ErrorDocument 404 /error.php
RewriteEngine On
RewriteRule ^([^/]*)-([^/]*)$ /index.php?$1$2
RewriteCond %{HTTP_HOST} ^reflap\.com [NC]
RewriteRule (.*) http://www.reflap.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /index.php?$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ /index.php?$1&$2 [QSA,L]
RewriteRule ^/facebook-chat/ http://chat.facebook.com/ [P]
ErrorDocument 404 /error.php
You need to put specifics in your rewrite rules above your generics - the ordering you have means that you are being redirected to index.php?new-page because you are matching a more general pattern first.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^reflap\.com [NC]
RewriteRule (.*) http://www.reflap.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)-([^/]*)$ /index.php?$1$2 [QSA,L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?$1&$2 [QSA,L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?$1&$2 [QSA,L]
RewriteRule ^/facebook-chat/ http://chat.facebook.com/ [P]
RewriteRule ^([^/]*)$ /index.php?$1 [QSA,L]
RewriteRule ^([^/]*)/$ /index.php?$1 [QSA,L]
ErrorDocument 404 /error.php
This should fix your problem - but there's a more general issue in this .htaccess - RewriteCond directives only apply to the RewriteRule immediately following them. They don't continue on to any other rule following. This means that all rules after the first one will apply regardless of if the file or directory really exists.
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond

Categories