htaccess rewrite traling slash to GET parameter - apache

I use this code to rewrite example.com/kat-something.html to example.com/kat.php?kat=something
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)-([^/]*)\.html$ $1.php?kat=$2 [QSA]
RewriteRule ^([^\.]+)$ $1.php [QSA]
But I want ?kat= to be / so example.com/kat/something.html to example.com/kat.php?kat=something so I tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)/(.*)\.html$ $1.php?kat=$2 [QSA]
RewriteRule ^([^\.]+)$ $1.php [QSA]
But it's not working. So how can I allow / to be an GET name?

You can try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^.]+)\.html$ $1.php?$1=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^./]+)/?$ $1.php [L]

Related

Directing Non-Slash URL to Slash

When I enter the website.com/seo link, I want to make a 301 redirect to the website.com/seo/ link. However, I don't want it to be broken in the codes I wrote in the current htaccess.
my htacces codes:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Based on your shown samples, could you please try following. Please clear browser cache before testing your URLs
RewriteEngine ON
RewriteBase /
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Use only one of these, do not use together!
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^\/(.*?)\/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
If that doesn't work, use:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^\/(.*?)\/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Remove the previous rules, and try once. If it works after removing previous rules, then let me know.

showing error on htaccess address rewrite

i am want to convert url:
/tld/?=com to /tld/com/
my full htaccess code is as below:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(whois|error)(?:\.php)?[\s?] [NC]
RewriteRule ^ /%1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(php?|jpg|gif|png|css|js|html|json|xml|eot|svg|ttf|woff|woff2|zip|csv|xlsx|webp|txt|gz|rar)$ [NC]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
##### Rewite #####
##RewriteRule ^([^/]*)\$ /tld.php?tld=$1 [L]
RewriteRule ^([^/]+)\$ /tld.php?tld=$1 [L]
but its show 500 error so please help
Have it this way:
Options -Indexes -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(whois|error)(?:\.php)?[\s?] [NC]
RewriteRule ^ /%1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(php?|jpg|gif|png|css|js|html|json|xml|eot|svg|ttf|woff|woff2|zip|csv|xlsx|webp|txt|gz|rar)$ [NC]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# ignore all rules below this for files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^tld/([\w-]+)/?$ tld.php?tld=$1 [L,QSA]

Remove parameters from url using .htaccess

I want to remove parameters from url,
I need change this:
http://example.com/?page=about
To this:
http://example.com/about
How?
This is my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
Try the following in htaccess :
RewriteEngine On
#Remove php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ /$1.php [NC,L]
#rewrite /about to /?page=about
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /?page=$1 [NC,L]
this will rewrite
/about
to
/?page=about
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?page=$1 [QSA,L]

.htaccess rewriterule for username like facebook

I am utilising following code for my current application
RewriteBase /
#for profile display
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([^/]+)/([^/]+)/([^/]+)/?$ /profile.php?username=$1&option=$2&source=$3 [QSA]
RewriteRule ^user/([^/]+)/([^/]+)/?$ /profile.php?username=$1&option=$2 [QSA]
RewriteRule ^user/([^/]+)/?$ /profile.php?username=$1 [QSA]
#for hiding.php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
What this will do is, it will treat URL www.domain.com/user/johndoe/photos/photoid-123 as www.domain.com/profile.php?username=johndoe&option=photos&source=photoid-123
Now, what I want to do is to remove /user/ part from the url and keep it like www.domain.com/johndoe/photos/photoid-123
I have tried modifying rewrite rule like RewriteRule ^([^/]+)/.... OR RewriteRule ^/([^/]+)/.... OR RewriteRule ([^/]+)/...., but it is giving me 500 Error
Any suggestions?
Have it this way:
DirectoryIndex index.html index.php
RewriteEngine On
RewriteBase /
#ignore files and directories from rewrites
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ profile.php?username=$1&option=$2&source=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ profile.php?username=$1&option=$2 [QSA,L]
RewriteRule ^([^/]+)/?$ profile.php?username=$1 [QSA,L]
#for hiding.php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !\.php$ %{REQUEST_FILENAME}.php [QSA,L]

.htaccess code giving error 500

I have this code that transform domain.com/Birthdays/birthdays.php?n=Something into domain.com/Birthdays/Something and also take off the .php and .html for other pages:
RewriteEngine on
RewriteRule ^Birthday/(\w+)$ /Birthdays/birthdays.php?n=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The problem comes when I add this:
RewriteEngine on
RewriteRule ^Birthdays/(.*)$ [L]
RewriteRule ^Birthday/(\w+)$ /Birthdays/birthdays.php?n=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It gives me an error (500) but if I change the (.*) to (\w+) it works. But I don't want that.
You can use:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Birthday/(\w*)$ /Birthdays/birthdays.php?n=$1 [L,QSA]
You are getting 500 due to looping error as your rules's pattern is matching starting and rewritten URI.