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
Related
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]
This is my .htaccess file, everything works so far but I can't manage to remove .php extension from files, every code that I tried from other answers just threw 500 or 404 error. Please advise where and what to add. Structure of the folders is localhost/myfolder/somefile.php
Just to be clear - localhost/myfolder/ is a root for my project.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News
RewriteRule ^product-news/([0-9]+[/])?$ posts.php? p=$1&cat=Product\ News
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1
This snippet will allow you to rewrite to remove php extensions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want your URL to have a trailing /, you can use this snippet:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Source
Removing extension, say; php or html in browser will let browser find it a little bit more effort to find the source file. if you need it so, this follows may help you:
UPDATED:
PHP:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/$ /$1.php
HTML:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/$ /$1.html
those will remove all extensions in your files (both php and html).
Note: see if server enables mod rewrite module/extension.
You should be using two .htaccess files. The first should go in your localhost root (to redirect requests to myfolder), and the second should go into myfolder (to match up routes against your PHP files):
Root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
myfolder .htaccess:
RewriteEngine On
RewriteBase /myfolder/
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News [L]
RewriteRule ^product-news/([0-9]+[/])?$ posts.php?p=$1&cat=Product\ News [L]
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials [L]
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs [L]
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1 [L]
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1 [L]
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1 [L]
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1 [L]
Note how I have also included [L] to stop it from doing anything else once it has found a match.
Just below your 301 rule add this rule:
RewriteEngine On
RewriteBase /myfolder/
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
I'm using Rewrite Rule in htaccess to get page data by slug.
I'm doing it manually by adding lines to .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !www\.example\.co\.il$ [NC]
RewriteRule ^(.*)$ http://www.example.co.il/$1 [L,R=301]
ErrorDocument 404 /404.php
RewriteRule ^עמוד-מסוים$ /page.php?id=1 [L]
RewriteRule ^דוגמא-לעמוד$ /page.php?id=2 [L]
RewriteRule ^דוגמא-נוספת$ /page.php?id=3 [L]
RewriteRule ^טקסט-כלשהו$ /page.php?id=4 [L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteRule ^blog$ /blog.php [L]
It's work good but I need to add new .htaccess line every time.
my DB table has Id and Slug (and other info)
and my page.php use $_GET['id'] and then select the other data from DB by this ID.
I tried somthing like this:
.htaccess:
RewriteRule ^page/([^/]*)$ /page.php?id=$1 [L]
page.php:
$id=$_GET['id'];
if(is_int($id)==false){ // if slug was enterd
$result=$mysqli->query("SELECT `id` FROM `pages` WHERE `slug`='$id' limit 1");
while($row=mysqli_fetch_assoc($result)){
$id=$row['id'];
}//query
}
But the URL not look like I want (adding /page/ to url)
I tried that also:
.htaccess:
RewriteRule ^/([^/]*)$ /page.php?id=$1 [L]
but it's make problem with other url's on my site (that are not connected to page.php)
How can I make it work without adding htaccess line every time?
EDIT:
when I try this .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !www\.example\.co\.il$ [NC]
RewriteRule ^(.*)$ http://www.example.co.il/$1 [L,R=301]
ErrorDocument 404 /404.php
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteRule ^blog$ /blog.php [L]
I get error on all pages, If I moving up the RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L] only the page.php file will work good (blog and contact will not work) and also non-WWW url's will not work good.
You could try adding some conditions, something like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
to filter out all existing files and directories.
Also, you might want to consider sanitizing your $id parameter before you stick it in a query
Found the soultion. This working perfect:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
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
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]