So, I have been reading up on mod_rewrite because I want to remove the .php extension and have the URL look different, so that the end user doesn't see all the arguments in the URL.
My goal:
/foo.php?p=bar
Looks like:
/foo/bar
Where foo and bar can be anything, numeric and chars.
I'm only working with .php files, so it doesn't have to work with .html and alike.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [F]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^/]+)/?$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?p=$2 [L]
Related
I have tried so many things in the past hour and nothing works.
I have a website called example.com
I have this code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ index.php?p=$1&i=$2 [N]
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=$1
When I go on example/forums/discussion-f3.html it works.
When I try /forums/introductions-f2.html, it redirects me to /index.php/?p=forums&i=introductions-f2. Also, http://craftshaft.org/forums/ sends me to http://craftshaft.org/index.php/?p=forums but when I do it without the slash and remove it from the code, it works, but I need the slash at the end so it looks like a folder.
Basically, I'd like to be able to view URLs like this:
/forums.html (to /index.php?p=forums)
forums/thread-name.html (to
index.php?p=forums&i=thread-name)
Place this code in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)(\.html)/?$ index.php?p=$1 [L,NC,QSA]
RewriteRule ^([\w-]+)/([\w-]+)\.html$ index.php?p=$1&i=$2 [L,NC,QSA]
My goal is to redirect any url such as http://mydomain.com/somename to http://mydomain.com/somename.html except for http://mydomain.com/name1 and http://mydomain.com/name2 For these two, I wish to redirect them to http://mydomain.com/main.php?g1=name1 (or name2, etc). If these later two have two or three more directories in the URL (i.e. http://mydomain.com/name1/val2/val3), I wish to add them as individual GET values such as http://mydomain.com/main.php?g1=name1&g2=val2&g3=val3. I would like the browser to keep showing the directory path, and not something like http://mydomain.com/somename.html.
Below is my unsuccessful attempt. How can I accomplish this? Thank you
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
## If the request is for a valid directory, file, or link, don't do anything
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !^/name1 [OR] RewriteCond %{REQUEST_URI} !^/name2
RewriteRule ^(.*)$ $1.html [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ ?p=$1&c=$2&v=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ p=$1&c=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ ?p=$1 [L,QSA]
</IfModule>
You have the right idea in most places, but you need to change the order of your rules to match more specific stuff before less specific stuff. Generally, I'm allowing a trailing / in the rules below via /?. Remove that from the end if you won't permit a trailing / to be matched.
RewriteEngine on
RewriteBase /
# This should be fine as you have it....
## If the request is for a valid directory, file, or link, don't do anything
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
# Reverse your rule order here.
# First match name1|name2 with an optional trailing /
# but nothing else following...
RewriteRule ^(name1|name2)/?$ main.php?g1=$1 [L,QSA]
# Two additional dirs
RewriteRule ^(name1|name2)/([^/]+)/([^/]+)/?$ main.php?g1=$1&g2=$2&g3=$3 [L,QSA]
# Three additional dirs
RewriteRule ^(name1|name2)/([^/]+)/([^/]+)/([^/]+)/?$ main.php?g1=$1&g2=$2&g3=$3&g4=$4 [L,QSA]
# Last, do the generic rule to rewrite to .html
# using [^.]+ to match anything not including a .
# You could be more specific with something like [a-z]+ if that
# corresponds to your expected input
RewriteRule ^([^.]+)$ $1.html [L,QSA]
If you wanted to check that the URI is not name1, name2 in a condition, use an [AND] which is implicit:
RewriteCond %{REQUEST_URI} !/name1
RewriteCond %{REQUEST_URI} !/name2
RewriteRule ^([^.]+)$ $1.html [L,QSA]
I know this is a pretty common task and has been discussed here a lot, but I still can't get it to work. After doing some research I added this code into my .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
But it still doesn't work and displays the .html on my pages.
What could be wrong?
Thanks
You need an additional rule to externally redirect foo.html to foo. Have your .htaccess like this:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]
I am trying to create a nice looking url for my calendar site.
But I can't get it to work properly.
This is my URL:
http://mydomain.com/admin/calendar?year=2013&month=november
...and I want it to look like this:
http://mydomain.com/admin/calendar/2013/november
The "calender"-folder is not a real folder and the .htaccess file is located in the admin-folder and not in the root of my website.
I am currently using this code:
RewriteEngine On
# Make sure not to rewrite real files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# Removes PHP extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# Make new URL
RewriteBase /admin/
RewriteRule ^([^/]*)/([^/]*)$ /admin/calendar/calendar?year=$1&month=$2 [L]
Any idea why this doesn't work, and how to get it to work? :D
Thanks :)
TheYaXxE
Try this code in your admin/.htaccess file:
RewriteEngine On
RewriteBase /admin/
# Make sure not to rewrite real files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Make new URL
RewriteRule ^calendar/([^/]+)/([^/]*)/?$ calendar.php?year=$1&month=$2 [NC,L,QSA]
# Removes PHP extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+?)/?$ $1.php [L]
Try this:
RewriteRule ^([^/]+)/([^/]+)/$ /admin/calendar/calendar?year=$1&month=$2
My .htaccess file is creating a redirect loop. I have searched online and tried lots of varieties including many from this site but still can't get it to work. The purpose of it is to remove the .html from the end of the URL.
Here is my code:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
Replace all of your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .html extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.+?)\.html?[/?\s] [NC]
RewriteRule ^ %1? [NE,R=301,L]
# To internally forward /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]
## append trailing slash if needed
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]