rewrite sub folder url query string - .htaccess

I am new to web development.
Below Htaccess code works fine in root directory files. While We try to rewrite the files in sub folder /test/downloadh.php. when i was changed below code will we can't get the root files links are notworking.
RewriteEngine on
RewriteBase /test/
Would you please suggest us, how to get the url
http://example.com/test/downloadh/the-house-on-the-borderland
to
http://example.com/test/downloadh.php?id=the-house-on-the-borderland
Working Code:-
# disable directory browsing
Options -Indexes
AddDefaultCharset UTF-8
FileETag MTime Size
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+album\.php\?Id=([^\s&]+) [NC]
RewriteRule ^ album/%1? [R=301,L]
RewriteRule ^album/([^/]+)/?$ album.php?Id=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+download\.php\?id=([^\s&]+) [NC]
RewriteRule ^ download/%1? [R=301,L]
RewriteRule ^download/([^/]+)/?$ download.php?id=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+downloada\.php\?linkb=([^\s&]+) [NC]
RewriteRule ^ downloada/%1? [R=301,L]
RewriteRule ^downloada/([^/]+)/?$ downloada.php?linkb=$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:downloadb\.php)?\?mlink=([^\s]+)&linkb=([^\s]+)? [NC]
RewriteRule ^ downloadb/%1%2? [R=301,L]
RewriteRule ^downloadb/([^/]+)/([^/]+)/?$ downloadb.php?mlink=$1&linkb=$2 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:songs\.php)?\?id=([^\s]+)&linkb=([^\s]+)? [NC]
RewriteRule ^ songs/%1/%2? [R=301]
RewriteRule ^songs/([^/]+)/([^/]+)/?$ songs.php?id=$1&linkb=$2 [L,QSA]
# CUSTOM ERRORPAGE
ErrorDocument 401 /error/test.php
ErrorDocument 403 /error/test.php
ErrorDocument 404 /error/test.php
ErrorDocument 500 /error/test.php
Error Code:-
RewriteCond %{THE_REQUEST} /downloadh(?:\.php)?\?id=([^\s&]+)\s [NC]
RewriteRule ^ downloadh/%1/? [R=302,L,NE]
RewriteRule ^downloadh/([^/]+)/?$ downloadh.php?id=$1 [NC,L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test/downloadh.php\?id=([^\s&]+) [NC]
RewriteRule ^test/downloadh\.php /%1? [R=301,L,NE]
RewriteRule ^downloadh/([^/]+)/?$ /test/downloadh.php?id=$1 [L,QSA]

Try this code :
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/(test/downloadh)\.php\?id=(.+)\sHTTP.*$ [NC]
RewriteRule ^ %1/%2? [L,R=301]
RewriteRule ^test/downloadh/([^/]+)/?$ test/downloadh.php?id=$1 [L]

Related

I need help configuring my htaccess file properly

so I got this script from codester and I wasn't able to install it cause I keep getting errors like ".htaccess misconfigured" can anyone detect the error for me please, here is the .htaccess code
RewriteEngine on
RewwriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteRule page/(.*)$ page.php?slug=$1 [L]
RewriteRule py$ _py.php [L]
RewriteRule payout$ pyt.php [L]
RewriteRule contact$ ct.php [L]
Options -Indexes
ErrorDocument 404 /404.php
Help
There is a spelling mistake in your second line, use
RewriteBase /
instead of RewwriteBase /
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
RewriteRule page/(.*)$ page.php?slug=$1 [L]
RewriteRule py$ _py.php [L]
RewriteRule payout$ pyt.php [L]
RewriteRule contact$ ct.php [L]
Options -Indexes
ErrorDocument 404 /404.php

Force redirect permanently on RedirectMatch from $_GET url to semantic url

I am doing some .htaccess settings to improve SEO with semantic URL's. Currently when you go to the semantic URL www.website.com/profile/exampleusername it works perfectly although i am still able to access the same page without any red alarms rining with the pure get request in the url. www.website.com/profile?username=exampleusername.
I have tried to user both the redirect and RedirectMatch but i can't get it to work.
Here is the relevant code from the .htaccess
RewriteCond %{THE_REQUEST} /profile\.php\?username=([^\s&]+)&buddy_identifier=([^\s&]+) [NC]
RewriteRule ^profile/%1/%2? [R=302,L]
RewriteRule ^profile/([^/]+)/([^/]+)/?$ profile.php?username=$1&buddy_identifier=$2 [L,NC,QSA]
I want to be able to go to www.website.com/profile?username=exampleusername and then be redirected to www.website.com/profile/exampleusername
Full .htacces
RewriteEngine on
Options -MultiViews +FollowSymLinks -Indexes
RewriteCond %{ENV:REDIRECT_STATUS} ^$
############# REMOVE FILE EXTENSION #############
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
#####
######## ONLY ACCESS BY HTTPS://WWW.* AND NO EXTENSION #############
#commented bcs of dev workspace localhost
#RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^/]+)\.php(\?|\ |$)
#RewriteRule ^ /%2 [L,R=301]
#RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
#RewriteCond %{HTTPS} off [OR]
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
#RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
############# INDEX RENAME #############
DirectoryIndex gamingbuddy.php
################### SEMANTIC URL ######################
### URL FOR GAMES.PHP ###
RewriteCond %{THE_REQUEST} /games\.php\?game=([^\s&]+)&game_style=([^\s&]+) [NC]
RewriteRule ^games/%1/%2? [R=302,L]
RewriteRule ^games/([^/]+)/([^/]+)/?$ games.php?game=$1&game_style=$2 [L,NC,QSA]
### URL FOR PRIVATE PROFILE ###
RewriteCond %{THE_REQUEST} /profile\.php\?username=([^\s&]+)&buddy_identifier=([^\s&]+)\s [NC]
RewriteRule ^profile/%1/%2? [R=302,L]
RewriteRule ^profile/([^/]+)/([^/]+)/?$ profile.php?username=$1&buddy_identifier=$2 [L,NC,QSA]
You can use these rules:
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} /profile(?:\.php)?\?username=([^\s&]+)\s [NC]
RewriteRule ^ /profile/%1? [R=302,L]
RewriteRule ^profile/([^/]+)/?$ profile.php?username=$1 [L,NC,QSA]

.htaccess mod_rewrite is messing up brackets [ ] in my URL

I changed a few things in my .htaccess file a few days ago and just now noticed that brackets in the URL are now messed up. Instead of:
url.com/admin/page?delete[]=12345&reasons[]=2
the URL shows as:
url.com/admin/page?delete%255B%255D=12345&reasons%255B%255D=2
What could be causing this? Here's my .htaccess file. It's driving me crazy!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=302,L,NE]
RewriteRule ^admin/page/?$ admin/page.php [L,NC]
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^.* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/profile?user=%1 [L,QSA]
RewriteRule ^([^/]*)-([0-9]+)\.html$ product?name=$1&id=$2 [L,QSA]
ErrorDocument 400 /errors/400.php
ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
Have your rules like this:
ErrorDocument 400 /errors/400.php
ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=302,L,NE]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302,NE]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1 [L,R=302,NE]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/profile?user=%1 [L,QSA,R=302,NE]
RewriteRule ^([^/]+)-([0-9]+)\.html$ product?name=$1&id=$2 [L,QSA]
RewriteRule ^admin/page/?$ admin/page.php [L,NC]
Keep redirect rules before rewrites and use NE flag in each redirect rule.
Make sure to clear browser cache before testing.

Getting value with htaccess and pagination

This url
example.com/videos/load.php?cat=video-category
would become
example.com/videos/video-category
Also, there is pagination url
example.com/videos/load.php?cat=video-category&p=2
and it would become
example.com/videos/video-category/page/2
I tried to do this.. but it doesn't work.
Here is my htaccess (into videos folder)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /videos/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteRule ^(\w+/+/)page/?$ $1 [R=301,L]
RewriteRule ^(\w+)(?:/[^/]+)?/?$ load.php?cat=$1 [L,QSA]
RewriteRule ^(\w+)/[^/]+/([0-9]+)/?$ load.php?cat=$1&p=$2 [L,QSA]
Put this code in your htaccess.
This should work
Options +FollowSymLinks
RewriteEngine on
RewriteBase /videos/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NE,L]
RewriteRule ^([^/]+)/?$ load.php?cat=$1 [L,QSA]
RewriteRule ^([^/]+)/page/?$ $1 [R=301,L]
RewriteRule ^([^/]+)/page/([1-9][0-9]*)$ load.php?cat=$1&p=$2 [L,QSA]

rewrite dynamic url

I have the following problem:
http://www.mydomain.com/articles.php?artid=89
should become: http://www.mydomain.com/mykeyword
I dont mind if the id remain in the url...like: http://www.mydomain.com/89/mykeyword
I have the following .htaccess for the moment:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(mydomain\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+news\.php [NC]
RewriteRule ^ mykeyword [R=301,L]
RewriteRule ^mykeyword/?$ news.php [L,NC]
The above part works like a charm.
Any help will be deeply appreciated.
Regards, Zoran
Append these rule at the bottom of your .htaccess:
RewriteEngine On
RewriteBase /turkexpo/
RewriteCond %{HTTP_HOST} ^(webone\.gr)$ [NC]
RewriteRule ^(.*)$ http://www.%1/turkexpo/$1 [R=302,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+turkexpo/news\.php [NC]
RewriteRule ^ mykeyword [R=302,L]
RewriteRule ^mykeyword/?$ news.php [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+turkexpo/articles\.php\?artid=([0-9]+) [NC]
RewriteRule ^ %1/mykeyword? [R=302,L]
RewriteRule ^([0-9]+)/mykeyword/?$ articles.php?artid=$1 [L,NC,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+turkexpo/companies\.php\?fairid=([0-9]+)&ehallid=([0-9]+) [NC]
RewriteRule ^ companies/%1/%2? [R=302,L]
RewriteRule ^companies/([0-9]+)/([0-9]+)/?$ companies.php?fairid=$1&ehallid=$2 [L,NC,QSA]
This will forward a URI of /89/mykeyword to /articles.php?artid=89 and externally redirect the opposite of it.
Once you make sure it is working change R=302 to R=301.

Resources