Replace parts of a URL with .htaccess Rewrite Rules - .htaccess

Good morning,
I´m want to change the urls of my website to google seo optimized ones. The way I have at the moment, does not work well. My website is available in 3 languages, English, German and French. The names of the sections are stored in the database. To select the language, tags are used [de][/de][en][/en][fr][/fr]
After changing the URL to the Seo one, that tags are still in the url, what looks not nice.
I changed actual that URL
http://localhost/AAMFULL/modules/merchandise/viewcat.php?cid=4
into this one
http://localhost/AAMFULL/merchandise/[de]Anziehsachen[/de][en]Clothings[/en][fr]Vêtements[/fr]/cat,4,0,200,0,list.html
No I have seen, it´s possible to give the htaccess a language tag, to get the right part of the URL. How does that work?
If that not work, how can I change the url to look nicer? like this:
http://localhost/AAMFULL/merchandise/de-Anziehsachen/en-Clothings/fr-Vêtements/cat,4,0,200,0,list.html
Or do you have another idea?
That´s the code inside my htaccess
htaccess rules file:
#SEO .Htaccess setting
RewriteRule ^merchandise.([a-z]+).([0-9.]+)/ modules/merchandise/index.php?seoOp=$1&seoArg=$2
RewriteRule ^merchandise/submit.html modules/merchandise/submit.php [L,NC,QSA]
RewriteRule ^merchandise/submit.php modules/merchandise/submit.php [L,NC,QSA]
RewriteRule ^merchandise/ratephoto.php modules/merchandise/ratephoto.php [L,NC,QSA]
RewriteRule ^merchandise/cat,([0-9]+),([0-9]+),([0-9]+),([0-9]+),([a-zA-Z0-9]+).html modules/merchandise/viewcat.php?cid=$1&uid=$2&num=$3&pos=$4&view=$5 [L,NC,QSA]
RewriteRule ^merchandise/rss,([0-9]+),([0-9]+),([0-9]+),([0-9]+),([a-zA-Z0-9]+).rss modules/merchandise/rss.php?cid=$1&uid=$2&num=$3&pos=$4&view=$5 [L,NC,QSA]
RewriteRule ^merchandise/index,([0-9]+),([0-9]+).html modules/merchandise/index.php?num=$1&pos=$2 [L,NC,QSA]
RewriteRule ^merchandise/rate,([0-9]+).html modules/merchandise/ratephoto.php?lid=$1 [L,NC,QSA]
RewriteRule ^merchandise/top,([0-9]+),([0-9]+).html modules/merchandise/topten.php?hit=$1&rate=$2 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/index.php modules/merchandise/index.php [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/comment_(.*?) modules/merchandise/comment_$2 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/photo.php(.*?) modules/merchandise/photo.php$2 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/viewcat.php(.*?) modules/merchandise/viewcat.php$2 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/ratephoto.php(.*?) modules/merchandise/ratephoto.php$2 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/list.tag.php(.*?) modules/merchandise/list.tag.php$2 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/view.tag.php(.*?) modules/merchandise/view.tag.php$2 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/submit.php(.*?) modules/merchandise/submit.php?$2&title=$1 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/submit,([0-9]+).html modules/merchandise/submit.php?cid=$2&title=$1 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/cat,([0-9]+),([0-9]+),([0-9]+),([0-9]+),([a-zA-Z0-9]+).html modules/merchandise/viewcat.php?cid=$2&uid=$3&num=$4&pos=$5&view=$6 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/rss,([0-9]+),([0-9]+),([0-9]+),([0-9]+),([a-zA-Z0-9]+).rss modules/merchandise/rss.php?cid=$2&uid=$3&num=$4&pos=$5&view=$6 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/(.*?)/edit,([0-9]+),([0-9]+).html modules/merchandise/editphoto.php?lid=$3&cid=$4 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/(.*?)/rate,([0-9]+),([0-9]+).html modules/merchandise/ratephoto.php?lid=$3&cid=$4 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/(.*?)/submit.php?cid=([0-9]+) modules/merchandise/submit.php?cid=$3&title=$1 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/(.*?)/comment_(.*?) modules/merchandise/comment_$3 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/(.*?)/photo.php(.*?) modules/merchandise/photo.php$3 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/(.*?)/viewcat.php(.*?) modules/merchandise/viewcat.php$3 [L,NC,QSA,P]
RewriteRule ^merchandise/(.*?)/(.*?)/ratephoto.php(.*?) modules/merchandise/ratephoto.php$3 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/(.*?)/list.tag.php(.*?) modules/merchandise/list.tag.php$2 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/(.*?)/view.tag.php(.*?) modules/merchandise/view.tag.php$2 [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/(.*?)/index.php modules/merchandise/index.php [L,NC,QSA]
RewriteRule ^merchandise/(.*?)/(.*?)/([0-9]+),([0-9]+).html modules/merchandise/photo.php?lid=$3&cid=$4 [L,NC,QSA]
RewriteRule ^merchandise/(.*?) modules/merchandise/$1 [L,NC,QSA]
The excemple Url are from my testsystem. The original URL of the live version from that excample is
https://kenner-mask.com/modules/merchandise/viewcat.php?cid=4
In the red section in the header you can see the title from the seo url and how it change with the different languages

Related

Enable a directory with .htacces

I have the following .htaccess config:
RewriteEngine on
RewriteBase /
RewriteRule ^model/([^/\.]+)/?$ /model/index.php?u=$1 [L,NC,QSA]
RewriteRule ^collections/([^/\.]+)/?$ /collections/index.php?c=$1 [L,NC,QSA]
RewriteRule ^general/([^/\.]+)/?$ /general/index.php?p=$1 [L,NC,QSA]
RewriteRule ^blog/([^/\.]+)/?$ /blog/index.php?e=$1 [L,NC,QSA]
RewriteRule ^blog/archive/ /blog/archive/index.php [L,NC,QSA]
What I need is just enable http://example.org/blog/archive/ url and now in such directory there is an index.php file.
The problem is that the above rule /blog/ take the action instead of the last one.
What should I do?
Change the order of your rules:
RewriteEngine on
RewriteBase /
RewriteRule ^model/([^/.]+)/?$ /model/index.php?u=$1 [L,NC,QSA]
RewriteRule ^collections/([^/.]+)/?$ /collections/index.php?c=$1 [L,NC,QSA]
RewriteRule ^general/([^/.]+)/?$ /general/index.php?p=$1 [L,NC,QSA]
RewriteRule ^blog/archive/ /blog/archive/index.php [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([^/.]+)/?$ /blog/index.php?e=$1 [L,NC,QSA]
However if /blog/archive/ is a real directory then you don't even need any rewrite rule if you add this line on top of your .htaccess:
DirectoryIndex index.php
This will load index.php by default for any directory.

RewriteRule is creating new URLs but not redirecting them from the dynamic ones

My website http://www.musicea.com.au has dynamic URLs. I have used .htaccess to rewrite the static URLs. When I copy the rewritten static urls and paste directly in the browser they open. But they are not redirecting automatically. My code is as given below.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page-([0-9]+)\$ content.php?pageid=$1 [L]
RewriteRule ^$ content.php?pageid=home
RewriteRule ^about$ content.php?pageid=1341369117 [L,NC]
RewriteRule ^music-lessons$ content.php?pageid=1344314997 [L,NC]
RewriteRule ^piano-lessons$ content.php? pageid=1347926925&getsubnavid=1344314997 [L,NC]
RewriteRule ^keyboard-lessons$ content.php?pageid=1347927089&getsubnavid=1344314997 [L,NC]
RewriteRule ^guitar-lessons$ content.php?pageid=1347927135&getsubnavid=1344314997 [L,NC]
RewriteRule ^violin-lessons$ content.php?pageid=1347927198&getsubnavid=1344314997 [L,NC]
RewriteRule ^content.php?pageid=home$ http://www.musicea.com.au/ [L,NC]
I think the URLs are rewritten properly but there's problem in redirecting.
Please suggest me.
Here is an example of what I meant in the comments. You only have a rule to internally rewrite to your PHP file, but you don't have a rule to redirect the use of the php file directly. So I'll show you an example of how to do that. I did one rule so that you can see. But because all your URI's don't have any mapping to your dynamic URL you will have to do a similar rule for each one of you rules.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page-([0-9]+)\$ content.php?pageid=$1 [L]
RewriteRule ^$ content.php?pageid=home
RewriteRule ^about$ content.php?pageid=1341369117 [L,NC]
#redirect direct request for content.php for music lesson
RewriteCond %{THE_REQUEST} [A-Z]{3,}\ /content\.php\?pageid=1344314997
RewriteRule ^ /music-lessons? [R=301,L]
RewriteRule ^music-lessons$ content.php?pageid=1344314997 [L,NC]
RewriteRule ^piano-lessons$ content.php? pageid=1347926925&getsubnavid=1344314997 [L,NC]
RewriteRule ^keyboard-lessons$ content.php?pageid=1347927089&getsubnavid=1344314997 [L,NC]
RewriteRule ^guitar-lessons$ content.php?pageid=1347927135&getsubnavid=1344314997 [L,NC]
RewriteRule ^violin-lessons$ content.php?pageid=1347927198&getsubnavid=1344314997 [L,NC]
Let me know if this helped.

Ensure rewrite-rule is used

I have created a simple rewrite URL like this:
RewriteRule ^messages$ /messages.php [L]
How do I force the user to use /messages and not /messages.php? I.e., if user queries messages.php, he'll be 301 redirected to /messages.
I have a long list of rewrite rules, like the one above.
RewriteEngine On
RewriteRule ^messages$ /messages.php [L]
RewriteRule ^events$ /events.php [L]
RewriteRule ^cv$ /cv.php [L]
RewriteRule ^profile/([^/]*)$ /profile.php?id=$1 [L]
RewriteRule ^ratings$ /ratings.php [L]
RewriteRule ^newsfeed$ /newsfeed.php [L]
RewriteRule ^logout$ /logout.php [L]
RewriteRule ^profile$ /profile.php [L]
RewriteRule ^ranking$ /rank.php [L]
RewriteRule ^find-study$ /search.php [L]
RewriteRule ^search/$ /search.php [L]
RewriteRule ^search$ /search.php [L]
RewriteRule ^invite-friends$ /invite-friends.php [L]
RewriteRule ^profile/([^/]*)$ /profile.php?id=$1 [L]
RewriteRule ^profile/([^/]*)/([^/]*)$ /profile.php?id=$1&programme=$2 [L]
RewriteRule ^student-requests$ /student-requests.php [L]
# Q&A
# view question
RewriteRule ^question(?:/([^/]+)(?:/([^/]*))?)?/?$ /question.php?id=$1&permalink=$2 [L]
RewriteRule ^question/([^/]*)/([^/]*)/([^/]*)$ /question.php?id=$1&permalink=$2&answer=$3 [L]
# manage question
RewriteRule ^ask(?:/([^/]+)(?:/([^/]*))?)?/?$ /manageQuestion.php?tag_id=$1&tag_type=$2 [L]
RewriteRule ^ask/([^/]*)/([^/]*)/([^/]*)$ /manageQuestion.php?tag_id=$1&tag_type=$2&tag_name=$3 [L]
RewriteRule ^editquestion(?:/([^/]+)(?:/([^/]*))?)?/?$ /manageQuestion.php?id=$1&second=$2 [L]
# questions
RewriteRule ^questions$ /questions.php [L]
RewriteRule ^questions/([^/]*)$ /questions.php?first=$1 [L]
RewriteRule ^questions/([^/]*)/([^/]*)$ /questions.php?first=$1&second=$2 [L]
RewriteRule ^questions/([^/]*)/([^/]*)/([^/]*)$ /questions.php?first=$1&second=$2&third=$3 [L]
RewriteRule ^questions/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /questions.php?first=$1&second=$2&third=$3&fourth=$4 [L]
When trying to solve this problem you probably encountered the problem of infinite loops. One way to prevent this is to use %{THE_REQUEST}, since this variable doesn't change on a rewrite. Therefore, if you use the following rule, it will change every url ending on .php to an url without it:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(messages|events|cv|ratings|etc)\.php\ HTTP
RewriteRule ^ %2 [R,L]
After testing that the rule works correctly, change [R,L] to [R=301,L] to make the redirect permanent. Doing this before everything works correctly makes testing a pain.
Edit: Instead of using (.*) you could use /(page1|page2|page3) for all the simple redirects. For each of the more complex ones you probably need to have a single redirect each in the form of:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /questions\.php\?id=([^&]+)&permalink=([^&]+)&answer=([^&]+)\ HTTP
RewriteRule ^ questions/%2/%3/%4 [R,L]

dot is not working in htaccess as defined in htaccess rules

I don't have much knowledge in .htaccess and I am struggling with regex concept in .htaccess.
I want same url concept as on facebook for user profile but dash(-), underscore(_), dot(.) this I want in url also currently my url rule is as follow:
RewriteRule ^([a-z,\_,A-Z,\_,\-,0-9.][!pagenotfound|home|abuse/*|user/*|start/*|login/*|account/*|message/*|find/*|cms/*|accountactivation/*|register/*]*)/?$ memberspersonalinfo.php?members=$1 [QSA]
This pattern have some error
but when in my browser domain.com/username is comming it's working fine but when url will be domain.com/test-test or test_test or test.test it redirects to `PAGE NOT FOUND PAGE
and in my .htaccess rule defined to follow that rule like abuse/* have dynamic id wherever I have used star (*) they are referencing dynamic value. When I will do rule like
RewriteRule ^([a-z,\_,A-Z,\_,\-,0-9.]*)/?$ memberspersonalinfo.php?members=$1 [QSA]
It's working fine but when I moveto home page then it calls memberspersonalinfo.php page it self.
I am trying to explain my Question again with htaccess script
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
DirectoryIndex index.html index.php
ErrorDocument 404 /pagenotfound
ErrorDocument 403 /pagenotfound
ErrorDocument 401 /slapme/pagenotfound
ErrorDocument 500 /pagenotfound
#index
RewriteRule ^index.html$ index.php [L]
RewriteRule ^index$ index.php [L]
RewriteRule ^home$ index.php [L]
#nopage
RewriteRule ^pagenotfound$ nopage.php [L]
#Regitration
RewriteRule ^register/createaccount$ userregistration.php [L]
#Regitration Confirmation
RewriteRule ^register/accountconfirmation$ registrationconfirm.php [L]
#Activation Regitration
RewriteRule ^accountactivation/([a-z,\_,A-Z,\_,\-,0-9]*)/([a-z,\_,A-Z,=,\_,\-,0-9]*)$ registrationactivation.php?UsercontactID=$1&AccountId=$2 [L]
#after login
RewriteRule ^user/main$ after_login.php [L]
#Payment Detail
RewriteRule ^user/myaccount$ useraccount.php [L]
#Logout
RewriteRule ^user/logout$ logoutuser.php [L]
#creategroup
RewriteRule ^start/creategroup$ http://domain.com/start/creategroup [L,R=301]
#Forgot Password
RewriteRule ^user/forgotpassword$ http://domain.com/user/forgotpassword [L,R=301]
#Login
RewriteRule ^login/userlogin$ http://domain.com/login/userlogin [L,R=301]
#Remove account
RewriteRule ^account/remove$ removeuseraccount.php [L]
#Send an email page
RewriteRule ^message/?$ contacttouser.php [L,QSA]
#Report any site user page
RewriteRule ^abuse/([0-9]*)/$ reportsiteuser.php?reportUserID=$1 [L,QSA]
#CMS FIND FRIENDS PAGE
RewriteRule ^find/friend$ http://domain.com/find/friend [L,R=301]
#CMS TERMS OF USE PAGE
RewriteRule ^cms/howitworks$ howitworks.php [L]
#CMS CREATE OWN SLAP PAGE
RewriteRule ^cms/createownslap$ createownslap.php [L]
#CMS BRAND OR PRODUCT PAGE
RewriteRule ^cms/brandorproduct$ brandorproduct.php [L]
#CMS APPS AND FEATURES PAGE
RewriteRule ^cms/appsandfeatures$ appsandfeatures.php [L]
this my full htaccess code everthing is working fine all rules are working but when i add
#group personal profile page page
RewriteCond %{REQUEST_URI} !/abuse(.*)
RewriteCond %{REQUEST_URI} !/user(.*)
RewriteCond %{REQUEST_URI} !/start(.*)
RewriteCond %{REQUEST_URI} !/login(.*)
RewriteCond %{REQUEST_URI} !/account(.*)
RewriteCond %{REQUEST_URI} !/message(.*)
RewriteCond %{REQUEST_URI} !/find(.*)
RewriteCond %{REQUEST_URI} !/cms(.*)
RewriteCond %{REQUEST_URI} !/accountactivation(.*)
RewriteCond %{REQUEST_URI} !/register(.*)
RewriteCond %{REQUEST_URI} !/social(.*)
RewriteCond %{REQUEST_URI} !/images(.*)
RewriteCond %{REQUEST_URI} !/css(.*)
RewriteCond %{REQUEST_URI} !/js(.*)
RewriteCond %{REQUEST_URI} !/userimages(.*)
RewriteRule ^([a-z,\_,A-Z,\_,\-,0-9.]+)/?$ memberspersonalinfo.php?members=$1 [QSA,L]
it always loads memberspersonalinfo.php

How to fix URL canonicalization with .htaccess?

I wan't the server to always redirect my URL's to format as "http://www.domain.com", even if the user write just "domain.com".
I could find some examples of this on the web, but I already have some fixes in .htaccess file and I don't know, where to put what, so it does't clash with the previous code.
Here's my .htaccess file:
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404
RewriteRule ^adminator/?$ adminator/login.php [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3&detail4=$4 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2 [QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?detail1=$1 [QSA,L]
What should I put in, so it does the URL redirect to "www"?
And one last question, is it all I have to do, so search engines don't have problems with the URL's?
I usually do the contrary.
you probably want www.example.com to forward to example.com -- shorter URLs
are sexier.
no-www.org/faq.php?q=class_b
Thanks to HTML5 boilerplate, I usually add :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
If you want to do that in your .htaccess :
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^adminator/?$ adminator/login.php [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3&detail4=$4 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2&detail3=$3 [QSA,L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?detail1=$1&detail2=$2 [QSA,L]
RewriteRule ^([^/\.]+)/?$ index.php?detail1=$1 [QSA,L]
Else, try the inverse RewriteCond and associate RewriteRule
Your [^/\\.] pattern simplifies to [^/.] as the period does NOT need escaping.

Resources