Here's the code I tried in the .htaccess file. it allows 0-9 a-z A-Z underscore and hyphen
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# User page
RewriteRule ^test/([0-9a-zA-Z_-]+) testtwo.php?u=$1 [NC,L]
But once I changed the last line of code as follows to accept "dot/period" it doesn't work
RewriteRule ^test/([0-9a-zA-Z_-.]+) testtwo.php?u=$1 [NC,L]
or
RewriteRule ^test/([0-9a-zA-Z_-\.]+) testtwo.php?u=$1 [NC,L]
Your advice highly appreciated. Thank you
Check this modified regex
RewriteRule ^test\/([0-9a-zA-Z_\-\.]+)$ testtwo.php?u=$1 [NC,L]
Related
I need help with my htaccess file. I added the following to remove all .php extensions which is working fine.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
However I have a directory /users which I want to rewrite the URL strings. The current URL is like this:
aero.site/maki/users/index.php?t=mf5cc
I want the URL to look like
aero.site/maki/mf5cc
I used the following rewrite rule but it's giving me Object not found error:
RewriteRule ^([^/.]+)$ users/index.php?t=$1 [L]
My complete htaccess content is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^/.]+)$ users/index.php?t=$1 [L]
Kindly help me with the correct rewrite rule for the last line to turn aero.site/maki/users/index.php?t=mf5cc into aero.site/maki/mf5cc
FYI: my htaccess file is in aero.site/maki directory
I finally got it to work. Thanks to #misorude. Here's my updated .htaccess content:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^/.]+)$ users/index?t=$1 [L]
My site has links that ent to .html or .php.
I would like to have clean urls without trailing slash at the end.
With
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
I have clean urls but with trailing slash at the end. Whatever I tried had no success..
Thank you!
To remove a trailing slash use:
RewriteEngine on
RewriteRule (.+)/$ /example/$1 [L,R=301]
Options is required by Many Hosting
Options +MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
If you need to add any other file for instance .txt just add this
RewriteRule ^([^\.]+)$ $1.txt [NC,L]
Thank you for replies!
This works perfectly:
Options +MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Thanks a lot!
Thanks for your help. I have been trying to solve this for an hour now.
I had this Htaccess that would rewrite /file.html to a script but decided to remove the extension and it turned out like this:
RewriteEngine On
#RewriteRule ^([^/]*)\.html$ /?file=$1 [L]
RewriteRule ^([^\.]+)$ /index.php?file=$1 [NC,L]
However, now I need it to work also with filename containing the dot (i.e. /file.with.dots) but that rule doesn't allow it.
This one works for files with dots but ending in .html
#RewriteRule ^(.*)\.html$ /index.php?file=$1 [NC]
This gets me an Internal Server Error
RewriteRule ^([^/]+)$ /index.php?file=$1 [NC,L]
You could just do this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/?$ /index.php?file=$1 [NC,L]
Let me know the result.
mywebsite.com/123 should display content via .htaccess of mywebsite.com/project.php?id=123 - unfortunately I changed the .htaccess rewriterule yesterday for anothe reason and now the redirection does not function (it goes to our 404-error page). Can you see what I've done wrong here? Many thanks!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9\-_]+)/?$ $1.php [NC,L]
Changing the last line to this should work:
RewriteRule ^([a-z0-9\-_]+)/?$ project.php?id=$1 [NC,QSA,L]
if you want the regex rule not to catch URLs that contain alphabetic characters, use
RewriteRule ^([0-9\-_]+)/?$ project.php?id=$1 [NC,QSA,L]
And for your new requirement:
RewriteRule ^([0-9\-_]+)/?$ project.php?id=$1 [NC,QSA,L]
RewriteRule ^([a-z\-_]+)/?$ new.php?id=$1 [NC,QSA,L]
I have an index file that builds content based on n PATH_INFO variables.
Example:
site.com/A/B/n/
should use index.php at either:
site.com/index.php?var1=A&var2=B&varN=n
- or -
site.com/index.php/A/B/n/
instead of:
site.com/A/B/n/index.php || which doesn't exist ||
So far I've tried a number of variations of:
RedirectMatch ^/.+/.*$ /
with no success.
I have an inelegant and unscalable solution here:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?p1=$1 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [NC,L]
Problems with this solution:
Inelegant and unscalable, requires manual line for each subdirectory
Fails with non alphanumeric characters (primarily +,= and &) ex. site.com/ab&c/de+f/
(note, even changing the regex to ^([a-zA-Z0-9_-\+\=\&]+)/?$ does little to help and actually makes it error out entirely)
Can you help?
Option 1: (site.com/index.php?var1=A&var2=B&varN=n):
Options +FollowSymLinks -MultiViews
RewriteEngine On
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
RewriteRule ^([^/]+)/?$ index.php?p1=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4 [QSA,L]
1. You had [NC] flag ... so there were no need to have A-Z in your pattern.
2. Instead of [a-zA-Z0-9_-\+\=\&] or [a-zA-Z0-9_-] I use [^/] which means any character except slash /.
3. [QSA] flag was added to preserve existing query string.
Option 2: (site.com/index.php/A/B/n/):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1 [L]
In reality, if you do not plan to show that URL anywhere (like, 301 redirect etc), the last line can easily be replaced by RewriteRule .* index.php [L] -- you will look for original URL using $_SERVER['REQUEST_URI'] in your PHP code anyway.
Adding the following will redirect all traffic (for files that do not exist) to the index page:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [NC]
Then, you can make your decision in index.php by parsing the 'REQUEST_URI'
RewriteRule ^/.+ / [R=301,L]
any url with any kind of path beyond root will be redirected to root.
^ = start of url
/ = a slash
.+ = 1 or more characters