i am trying to use RewriteRules to get clean urls using my HTACCESS file.
here is what i have so far
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
The above code takes a url that looks like this company.com/about.php and turns it into company.com/about/ so all my links url are like this "/about/" i didnt add the .php because of the rewrite rule.
what i am trying to do now is add a rule that will clean my url when parameter is passed. for example
company.com/about/?profile=member_name i want it to look like company.com/about/member_name
i have tried the two rewrite code below but it doesn't work.
RewriteRule ^([a-zA-Z0-9]+)$ /our_work.php/ [L]
RewriteRule ^/our_work.php([^/\.]+)/?$ ?project=$1 [L]
please keep in mind that my file extension is already being striped from the url.
Please help
Thank you in advance
Apache provides documentation on the use of RewriteRule that you should read before proceeding.
In this particular case, you can rewrite /about/{member_name} to /about.php?profile={member_name} by implementing this rule:
RewriteRule ^about/([a-zA-Z0-9]+)(/)?$ about.php?profile=$1 [L]
The rule states: The requested URL must begin with about/ and be followed by letters and numbers (match #1) and may be proceeded by an additional ending forward slash (match #2); and it will be substitued with a URL about.php with profile query string's value as match #1. The L flag indicates that any rules that follow this rule should be ignored.
Attempting to "cascade" rules will not work with your current set of rules because the /about/{...} is not matched by the first rule.
Related
I ned to change this url
https://halopredictions.com/blog/index.php?url=german-bundesliga-prepare-to-return-on-9-may
to
https://halopredictions.com/blog/german-bundesliga-prepare-to-return-on-9-may
This is my .htaccess that am currently using
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ index.php?url=$1 [NC,L]
I have already enabled mod rewrite
The htaccess is on this path ...\blog\.htaccess
Any help I will highly appreciate
Before proceeding with the answer, if I understand correctly you want to redirect from https://halopredictions.com/blog/german-bundesliga-prepare-to-return-on-9-may To https://halopredictions.com/blog/index.php?url=german-bundesliga-prepare-to-return-on-9-may.
Since SEO Urls are used by the visitors, we have to convert them to actual path.
The below RewriteRule will be sufficient to achieve the above condition.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ blog/index.php?url=$1 [NC,L,QSA]
Explanation:
^blog/([A-Za-z0-9-\+]+)/?$ - This part is what you want to rewrite from. Since, your URLs will be like in the pattern https://halopredictions.com/blog/german-bundesliga-prepare-to-return-on-9-may, we need to get the url part after blog/.
I have used a regular expression ([A-Za-z0-9-\+]+)/? to match the url part and it can be referrenced using $1.
Now we have to use $1 in our new url. For that, we have written the second part of RewriteRule where you can assign the referrence.
blog/index.php?url=$1 - Now, as you would assume we are using the $1 reference after index.php?url=, so that it will append it to the URL Query param and should lead to a valid path.
By the way, ^ this is used to indicate the start and $ for the end.
I was getting problem in htaccess. I have tried a lot examples but yet not able to write perfect Rewrite Rules as I want. Please help me.
http://domainname.com/sub-category.php?mc=eatry
to
http://domainname.com/eatry
AND
http://domainname.com/explorelisting.php?mc=eatry&&sc=restaurant
to
http://domainname.com/eatry/restaurant
.htaccess (I have tried)
RewriteEngine On
RewriteRule ^/(.*) ./sub-category.php?mc=$1
RewriteRule ^/(.*) ./explorelisting.php?mc=$1&&sc=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A few things to note.
In the URL you don't need && only & when specifying another parameter.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+) sub-category.php?mc=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+)/(\w+) explorelisting.php?mc=$1&sc=$2 [QSA,NC,L]
The regular expression assumes that the query will only accept any letter - capital or lowercase.
RewriteCond is used to check that the URL is not a directory and not a file, if it isn't, then it rewrites to the rule.
This is used because the URL http://www.example.com/index would get redirected to http://www.example.com/sub-category.php?mc=index which is undesired.
It might be prudent to restructure your rule. Perhaps having the URLs looking like http://www.example.com/category/eatry and then modifying the first rule to being RewriteRule ^category/(\w+) sub-category.php?mc=$1 in order to prevent ambiguity.
The flags (what is at the end of each rule in square brackets) are as follows:
QSA - Query String Append:
This enables you to have the parameters at the end of the string (anything following the ?).
NC - No Case:
This means that your rule is case insensitive.
L - Last:
This will terminate the htaccess if the rule is matched.
I currently use $_GET['base'] to determine which homepage that the user visits.
This results in localhost/?base=administrator or localhost/?base=guest
I am also using this to control which page is the user at, such as
localhost/?base=guest&page=register
Is there any way to use mod_rewrite, or htaccess, to change how this system works?
Modifying my code is not an issue, is this possible?
EDIT:
I am trying to achive this:
localhost/?base=guest to localhost/guest
localhost/?base=admin to localhost/admin
localhost/?base=guest&page=register to localhost/guest/register
Below is my htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
Will the document path affect how it is being called? As I am using a case loop to include which items are needed.
This, however, works for localhost, but it will loop every other address to main.
RewriteEngine On
RewriteRule ^$ /index.php?base=guest[L]
But did not give a result as expected.
Your rules in .htaccess need to be in reverse order, like below:
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
That is because if it is kept in the order you have it, both localhost/?base=guest&page=register & localhost/?base=administrator will match the rule RewriteRule ^([^/]*)$ /?base=$1.
Having them in reverse order ensures that the first rule is matched only for localhost/?base=guest&page=register. It won't match the first rule for localhost/?base=administrator. I hope that helps.
You need to exclude your existent files and folders from the rule
RewriteEngine On
# if the request is a dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
# or file
RewriteCond %{REQUEST_FILENAME} -f
#do nothing
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
So you can use this simple code:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?base=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?base=$1&page=$2 [L]
\w will match symbols a-z, 0-9 and underscore _, I think those characters are enough for your case, but if you need expansion it will be easy
Also in this case you don't need to change your code, because you still get base and page parameters in the $_GET array
UPDATE:
to disable query string params page and base (other params may be needed) add these two lines to the code at the bottom:
RewriteCond %{THE_REQUEST} (\?|&)(page|base) [NC]
RewriteRule .* - [L,R=404]
I need some help to write a rewrite rule : I tested many many things but i guess i'm doing something wrong.
I need to rewrite this kind of url:
this is the FROM url :
http://website.com/a-section-a/a-section-b/a-section-c/99999-name-name2#
to:
this is the TO url :
http://website.com/index.php/newsection/99999-name-name2
I tried many thing but actually i get it:
RewriteRule /index.php/newsection/ \/([a-z]+([-]|[\/]))+
But not working ( rewrite engine ON ).
edit : The url should redirect to the TO page AND rewrite it.
A bit unclear on how your rewrite rule should work due to your syntax and the odd placement of /index.php/, but try using this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/(.*)?$ /index.php/newsection/$4 [R,L,NC]
Just so you understand how it works, the RewriteEngine On simply tells Apache to turn on the rewrite rule engine in the ruleset. The RewriteCond %{REQUEST_FILENAME} !-f assures the rule only kicks in of there isn’t a file with the same name. Similarly, the RewriteCond %{REQUEST_FILENAME} !-d assures that the rule does not kick in if there isn’t a directory with the same name.
Now the actual RewriteRule breaks down like this:
Each ([a-z0-9-]+) represents a segment of the URL path. It only matches the letters a-z (case insensitive) & numbers 0-9 as well as the - character.
The / designates each path part like a real URL.
The last part of the path is (.*)?$ which will catch anything`.
The area past the regex stuff that matches the URL is the redirect destination with $4 matching the last thing captured by the regex stuff.
And the [R,L,NC] are Apache rewrite rule flags that equate to: R means redirect, L means last meaning the ruleset stops processing & NC means match the rule with “no case” (aka: case-insensitive).
try below code,
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.html HTTP/
RewriteRule ^index.html$ http://www.yoursite.com/ [R=301,L]
i have already this rewrite rule in my htaccess
RewriteCond $1 !^(index\.php)
RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?id_user=$1&page=$2 [L]
http://www.mydomain.com/index.php?id_user=user&page=news
http://www.mydomain.com/user/news.html
Now i have a problem for news details and event details.
for have an url like this (only for page news):
http://www.mydomain.com/user/details-news/category/title-news.html
and this (only for page event)
http://www.mydomain.com/user/event/title-event.html
Can i insert new rule in htaccess for have these rules or i must edit my actually rules?
My url is the same for all query
index.php?id_user=user&page=details-news&category=category&title=title-news
or
index.php?id_user=user&page=event&title=title-event
Any help would be greatly appreciated!
You actual rule match only against:
^([^/]*)/([^/]*)\.html$
urls.
Which means only one / inside. So you can add other rules with more slash, without the need to modify this actual existing rule.
The only problem would be if you want to add other rules with one /. But this is not the case.
You can use these rules:
# skip further rules for files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/.]+)\.html$ /index.php?id_user=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)\.html$ /index.php?id_user=$1&page=$2&title=$3 [L,QSA]
RewriteRule ^([^/]+)/^([^/]+)/([^/]+)/([^/.]+)\.html$ /index.php?id_user=$1&page=$2&category=$3&title=$4 [L,QSA]