htaccess code remove www and id and php in sub directory - .htaccess

with this code and clean URL like (example.com) I could add www and remove id in url.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But now my root of website is in a folder.
so my new url is www.examole.com/newfolder
how can add newfolder to top code?

You can use this .htaccess in /newfolder:
RewriteEngine On
RewriteBase /newfolder/
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ %1%2? [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]

Related

How to redirect a URL and then rewrite it to same url with htaccess but from the root?

This code below for rewrite/redirect from localhost/moneyworld/exchange?title=BTC_PMUSD to localhost/moneyworld/BTC_PMUSD but i want to rewrite/redirect from www.domain.com/exchange?title=BTC_PMUSD to www.domain.com/BTC_PMUSD Without also /moneyworld/
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(moneyworld)/exchange\?title=([^\s]*)\s [NC]
RewriteRule ^ /%1/%2 [NE,QSD,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /moneyworld/exchange?title=$2 [L]
Thanks
The problem was that i was forwarding exchange.php to exchange after the url rewrite rules so simply the code below solved it
RewriteEngine ON
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/exchange\?title=([^\s]*)\s [NC]
RewriteRule ^ /%1 [NE,QSD,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /exchange?title=$1 [L]

creating sef links and changing plus sign (+) with dash (-)

I've searched the web and I tried to solve problem but I could not figure out how.
I'd like to redirect dizisozluk.net/words to dizisozluk.net/ne-demek/anlami/words and change plus (+) sign to dash (-) sign. For example
dizisozluk.net/family+tree
to
dizisozluk.net/ne-demek/anlami/family-tree
My old code is here
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{THE_REQUEST} /search\.php\?word=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ search.php?word=$1 [QSA,L]
How can I change this?
Thanks in advance.
Have it this way:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} !\s/+ne-demek/anlami/ [NC]
RewriteRule (.*) https://%{HTTP_HOST}/ne-demek/anlami/$1 [R=301,L,NE]
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule .* https://www.%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{THE_REQUEST} /search\.php\?word=([^\s&]+) [NC]
RewriteRule ^ /ne-demek/anlami/%1? [R=301,L]
# recursive rule to replace space by - from URI OR query string (anywhere)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ne-demek/anlami/(.+)$ search.php?word=$1 [QSA,L]

I have 3 rules on .htaccess that is not function at same time Friendly URLs, Force HTTPS and Not WWW

Friendly URLs
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ ?url=$1
Forcing 'https://'
RewriteEngine On
RewriteCond %{HTTP:CF-Visitor} !'"scheme":"http"'
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1 [L]
Suppressing www. at the beginning of URLs
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
I'm using CloudFlare and when I activate all, it's return a loop.
Have your rules like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ ?url=$1 [L,QSA]
Make sure to test this after clearing your browser cache.

replace ? by / in urls and make them more friendly

hello all i have a url like
http://www.domain.com/dash?do=about&w=me
but i would like to make it like
http://www.domain.com/dash/about/me
i have these things in my htaccess file.
RewriteEngine On
RewriteBase /
Options All -Indexes
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
ErrorDocument 403 /www.domain.com/error404.php
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# redirect /profile?eid=1 to /eid/1
RewriteCond %{THE_REQUEST} \s/+profile(?:\.php)?\?(eid)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internally rewrite /eid/1 to /profile.php?eid=1
RewriteRule ^(eid)/(\d+)$ profile.php?$1=$2 [L,QSA,NC]
# redirect /dept?did=1 to /did/1
RewriteCond %{THE_REQUEST} \s/+dept(?:\.php)?\?(did)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internally rewrite /did/1 to /dept.php?did=1
RewriteRule ^(did)/(\d+)$ dept.php?$1=$2 [L,QSA,NC]
# redirect /job-details?job=1 to /job/1
RewriteCond %{THE_REQUEST} \s/+job-details(?:\.php)?\?(job)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internally rewrite /job/1 to /job-details.php?job=1
RewriteRule ^(job)/(\d+)$ job-details.php?$1=$2 [L,QSA,NC]
RewriteCond %{THE_REQUEST} \s/+enterprise\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ enterprise.php?url=$1 [L,QSA]
can any one suggest something to achieve this i have ?do=about&w=me comon everywhere and just need to do it like /about/me after the page.
It is slowly becoming difficult to maintain due to so many rules but here it is:
ErrorDocument 403 /www.domain.com/error404.php
Options All -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.*$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# redirect /profile?eid=1 to /eid/1
RewriteCond %{THE_REQUEST} \s/+profile(?:\.php)?\?(eid)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internally rewrite /eid/1 to /profile.php?eid=1
RewriteRule ^(eid)/(\d+)$ profile.php?$1=$2 [L,QSA,NC]
# redirect /dept?did=1 to /did/1
RewriteCond %{THE_REQUEST} \s/+dept(?:\.php)?\?(did)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internally rewrite /did/1 to /dept.php?did=1
RewriteRule ^(did)/(\d+)$ dept.php?$1=$2 [L,QSA,NC]
# redirect /job-details?job=1 to /job/1
RewriteCond %{THE_REQUEST} \s/+job-details(?:\.php)?\?(job)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internally rewrite /job/1 to /job-details.php?job=1
RewriteRule ^(job)/(\d+)$ job-details.php?$1=$2 [L,QSA,NC]
RewriteCond %{THE_REQUEST} \s/+enterprise\.php\?url=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# /dash?do=about&w=me => /dash/about/me
RewriteCond %{THE_REQUEST} \s/+([^?]+)(?:.php)?\?do=([^&\s]+)&w=([^&\s]+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1.php?do=$2&w=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ enterprise.php?url=$1 [L,QSA]

remove id and index.php via htaccess in subdirectory

current URL:
domain/index.php?id=p123
I want:
add www
remove:index.php
remove 'id' form URL
I do it this way:
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
it works if domian is without sub directory.
how can I change this code to support sub directory too like this:
domian/subdirectory/index.php?id=p123
I'll go about it this way:
First, redirect non-www URLs to www ones:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Rewrite the friendly URLs to respective index.php files:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*/)?([^/]+)$ /$1index.php?id=$2 [L]
Next, handle the raw URIs with index.php in them:
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
The last rule will also take care of URLs like domain.com/dir/?id=sth and domain.com/dir/index.php?id=sth
You can accomplish this with the following changes:
RewriteCond %{THE_REQUEST} \ /(.*/|)(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.*/|)([^/]*)$ $1index.php?id=$2 [L]
We know that %{THE_REQUEST} has the format GET /path/to/index.php?id=12312412 HTTP/1.1 or something along those lines. In the first rule we match \, which is a literal space, then the beginning slash that is always there. (.*/|) will match everything until index.php, or exactly nothing. The same trick we do in the second rule. I have added RewriteCond %{REQUEST_URI} !index\.php to prevent an infinite loop when index.php does not exist for a particular directory.

Resources