I am trying to write my .htaccess file so that all pages of the website (index.php, sports.php, team.php and about.php) appear as domain/index. team.php also occasionally takes in a $_GET variable which i am trying to show as domain/team/getvariable. I have came up with the following .htacces:
Options +FollowSymLinks
DirectoryIndex index.php team.php sports.php about.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R] # <- for test, for prod use [L,R=301]
#RewriteRule ^s/(.*)$ ./team.php?s=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
This meets all my requirements on WAMPP, however hosting the website using XAMPP behaves differently; the redirection to team/getvariable succeeds but throws a 404 error, the rest of it works well. You can also see that I have tried this one with a different approach but it behaves the same way as the current line. What am I doing wrong?
Solution
I changed the last line to
RewriteRule ^(.*)$ team.php?s=$1 [NC]
You are rewriting /team/variable to /team/variable.php. I guess this occurs your 404 error.
So I think you want to rewrite to something like /team.php?v=variable? Then this should work:
RewriteRule ^([^/]+)/([^/]+)/$ $1.php?v=$2
Why it works on WAMPP is another question...
Related
Am trying to access a blog section of my website https://lordmanadventures.com/viewblog/1 but i keep on getting a 500 Internal Server Error.
Am thinking the problem is with my .htaccess file. The weird thing is that if i change the url to this https://lordmanadventures.com/viewblog.php/1 , it works. Below is my .htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ viewblog.php?blog-id=$1 [L,QSA]
I have looked through various sites and solutions but can't seem to get it working. Any help is appreciated.
Okay i finally got it working. It seems that the way the rules are arranged from top to bottom matters. I removed the rewrite rule for the blog from the bottom, then added the page name. Then moved #SJacks answer of removing extension to the bottom et voilĂ !
# Start rewrite rules
RewriteEngine on
RewriteBase /
# Blog
RewriteRule ^viewblog/([a-zA-Z0-9_-]+)$ viewblog.php?blog-id=$1 [L]
# Remove php extension
rewritecond %{SCRIPT_FILENAME}.php -f
rewriterule [^/]$ %{REQUEST_URI}.php [QSA,L]
rewriterule ^(.*)/$ $1 [R=301,L]
Thanks for the assistance guys. Much appreciated.
Here's how to properly remove the PHP extension. After this specific page rewrites can be made. I didn't include anything on that because this should solve your problem. Let me know how you get on.
# Start rewrite rules
RewriteEngine on
RewriteBase /
# Remove php extension
rewritecond %{SCRIPT_FILENAME}.php -f
rewriterule [^/]$ %{REQUEST_URI}.php [QSA,L]
rewriterule ^(.*)/$ $1 [R=301,L]
I have a site with 3 scripts, namely viewgames.php, play.php and users.php present at the root directory. I also have a .htaccess file in the same (root) directory.
My .htaccess code that cleans URLs is :-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^search/(.*)$ ./viewgames.php?search=$1
RewriteRule ^category/(.*)$ ./viewgames.php?cat=$1
RewriteRule ^users/(.*)$ ./users.php?action=$1
RewriteRule ^play/(.*)$ ./play.php?gn=$1
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
ErrorDocument 404 /pagenotfound.php
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
When I was developing in localhost (XAMPP), I got no problem in accessing the clean URLs (using the above htaccess code) such as localhost/MySite/play/foo, localhost/MySite/category/action, localhost/MySite/search/lolol or localhost/MySite/users/myaccount.
It clearly means that the above .htaccess code was working like a charm in my localhost server. Then I uploaded the above code to a shared Apache-based linux server, and encountered some weird problems -
https://example.com/category/blabla was working fine, and so was https://example.com/search/blabla
But the URLs https://example.com/play/xyz and https://example.com/users/anyaction stopped working. By that I mean, the page was loading fine, the site was there, but no $_GET values were passed to the PHP script. Thus, I am not able to get the value of parametes gn and action. (see above code)
I even tried by var_dump-ing the $_GET array in the scripts, and they returned an empty array, meaning that no values were sent to the script.
Please check my .htaccess code. I am completely and utterly new to .htaccess, and the above code is merely a copied one. I will really appreciate any help in the matter.
Have it like this:
ErrorDocument 404 /pagenotfound.php
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^search/(.*)$ viewgames.php?search=$1 [L,QSA,NC]
RewriteRule ^category/(.*)$ viewgames.php?cat=$1 [L,QSA,NC]
RewriteRule ^users/(.*)$ users.php?action=$1 [L,QSA,NC]
RewriteRule ^play/(.*)$ play.php?gn=$1 [L,QSA,NC]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-z]+)/?$ $1.php [L,NC]
It is important to keep MultiViews options off to avoid content negotiation feature causing issues for mod_rewrite module.
I have a PHP script running on apache server (debian). The problem is the "RewriteCond" doesn't seem to be working while rewrite_mod is enabled. Just a couple of instructions in the .htaccess are working, and this is why I'm encountering errors through the routing process and the server keeps saying that some routes haven't been found.
IndexIgnore *
Options -Indexes
RewriteEngine on
RewriteRule ^(config/|config\.php) - [F,L,NC]
RewriteRule ^article/(.*)/? index.php?article&do=view&title=$1 [L,QSA]
RewriteRule ^thesis/(.*)/? index.php?thesis&do=view&title=$1 [L,QSA]
====================================================
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} $
RewriteCond $1 !^(index)
RewriteRule ^(.*)/? index.php?$1&route=true [L,QSA]
====================================================
RewriteRule ^me/? index.php?me [L,QSA]
RewriteRule ^home/? index.php?home [L,QSA]
RewriteRule ^layout1.php index.php?layout1 [L,QSA]
For instance :
domain/foo
It says foo not exist, But
domain/home
is working with no errors
EDIT :
Now it got a portion of my problem working, I tried domain/FOO uppercase like thas and it seemed to work! Why is it to be working with uppercase? What can I do to get this to work in lowercase as well ? The foo.php is into the folder
Thank you
My apologizes if this has been answered but I haven't found anything that works for me. This is on a cheap godaddy shared server, php based.
I have tried a few RewriteRules but to no avail.
Basically, if you are linked to something like :
www.example.com/items/apple
It would keep that in the url, but instead go to:
www.example.com/items
In the items page, I would be able to get the /apple part and do with it as I will. Either having it passed in through parameter like ?dir=apple or just as the /apple. My problem right now is the redirecting. My .htaccess file looks like this
RewriteEngine On
Options -Multiviews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^item/([^/]+) /item?dir=$1 [NC]
Try adding an additional condition and/or switch the order:
RewriteEngine On
Options -Multiviews
RewriteRule ^item/([^/]+) /item?dir=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have a setup that sets variables for the index page, but it also does the same for directories and I don't want it to do that. Here's my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC]
RewriteRule ^([a-zA-Z0-9]+)$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)$ index.php?name=$1&page=$2
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)/$ index.php?name=$1&page=$2
RewriteRule ^php/$ error/
Now, with this setup, if I type in mysite.com/login it will redirect to the index.php page and set login as the name variable. How do I make it to where it ignores the directories? This is frustrating me and I've looked through this site for over an hour for an answer and can't find a similar question (I might suck at that too, though. haha!).
Also, if you look at the last RewriteRule, you can see that I'm trying to redirect any attempt to access my php/ folder to my error/ folder. This is also not working.
RewriteCond only applies to the immediately following RewriteRule. Also, you can combine lines 3&4, and 5&6 respectively, by using /? on the end, which makes the / optional (regex).
Your file could be similar to this:
RewriteEngine On
#the following line will not rewrite to anything because of "-", & stop rewiting with [L]
RewriteRule ^(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js)/?(.*)$ - [L,NC]
RewriteRule ^php/?(.*)$ error/ [L,NC]
RewriteRule ^([^/]+)/?$ index.php?name=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?name=$1&page=$2 [L]
You may be interested in the Apache Mod_Rewrite Documentation.
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC] !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to either not execute the rule on a directory or a file
More info on the mod_rewrite documentation pages, search for CondPattern