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
Related
RewriteEngine on
RewriteRule ^admin/api/(.*)$ admin/api.php?method=$1 [QSA,L]
RewriteRule ^admin/(.*)$ admin/index.php [QSA,L]
RewriteRule ^api/(.*)/$ api.php?method=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpg|png|gif|svg|eot|ttf|woff)$ [NC]
RewriteRule (.*) index.php [QSA,L]
I want separate API entry point for admin module, but rewrite does not work as I want. What's wrong with rewrite rules?
host.com/page - works (/index.php),
host.com/admin/page - works (/admin/index.php),
host.com/admin/api/section - executes /index.php (but expected /admin/api.php)
That is because your rules are looping as admin/.* pattern also matches admin/api.php. Have rules like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^admin/api/([^.]+)$ admin/api.php?method=$1 [QSA,L]
RewriteRule ^admin/([^.]+)$ admin/index.php [L]
RewriteRule ^api/([^.]+)/$ api.php?method=$1 [L]
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpg|png|gif|svg|eot|ttf|woff)$ [NC]
RewriteRule (.*) index.php [L]
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]
I made a minor change in my .htaccess file and I'm now receiving a 500 internal server error. I changed it back to how it was before the error but, the error is still there.
Here is the file:
ErrorDocument 404 /404-error-page.php
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule \.(css|jpe?g|gif|png)$ - [L]
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^dsa-office /(.*)/ dsa_office.php?location=$1
RewriteRule ^([^/\.]+)/?$ /$1.php [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /$1.php?location=$2 [L]
I edit one of the last 3 lines. Any ideas? Thanks
This rule looks suspicious:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^dsa-office /(.*)/ dsa_office.php?location=$1
It has space in the matching patter between ^dsa-office and /(.*)/
Change this rule to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^dsa-office/(.*?)/?$ dsa_office.php?location=$1 [L,QSA]
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]
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