have a unique number in the url - do not match - .htaccess

This is how I would like to get my url to look at it any other way than before.
The problem is such that I must have forum and after forum content unique number that is in the database and after the url of the content.
I could imagine that it looked like this:
/forum/1/hello-world-stackoverflow-danmark/
now on my .htaccess file:
RewriteRule ^forum/([^/.]*)/([^/.]*)/?$ /forum-s.php?id=$1&url=$2 [L]
and now it:
www.hello-world.com/forum/velkommen-til-traenigsmakker---sjaelland/
I will have its to
www.hello-world.com/forum/1/velkommen-til-traenigsmakker---sjaelland/

Be sure to have RewriteEngine On
The code you have should work, you can optimize it like this to ensure the numbers.
RewriteRule ^forum/([0-9]+)/([^/.]*)/?$ /forum-s.php?id=$1&url=$2 [L]

Related

remove middle part of query from url with htaccess

searched on stackoverflow a bit, but nothing helpfull.
I want to do the following via htaccess.
I have this url:
site.com/location/usa/ca/los-angeles
and I want this:
site.com/location/ca/los-angeles
Basicly I want to remove the abbreviation of the country. Keep in mind, I have more then one country.
Any ideas?
To change all link like /location/1/2/3 to /location/2/3.
Use in your : /location/.htaccess:
RewriteEngine on
RewriteRule ^[^/]+/([^/]+/[^/]+)/?$ $1 [R=301,L]

.htaccess remove duplicate part of a URL

some bug in my online catalog SEF URL generation created a situation where some category slug is added to the URL twice. to fix this i would like to use .htaccess to remove the this part of the URL and have the whole URL shift up one level including URL's which also include a product under them.
the duplicate occurrence is of the sub-category painting-tools under the category tools
so the wrong URL looks like this:
/store/items/catalog/tools/painting-tools/painting-tools
or
/store/items/catalog/tools/painting-tools/painting-tools/{some product}
instead of being the correct URL like this:
/store/items/catalog/tools/painting-tools
or
/store/items/catalog/tools/painting-tools/{some product}
and the URLs
/store/items/catalog/tools/painting-tools/
or
/store/items/catalog/tools/painting-tools/{some product}
should actually be like this
/store/items/catalog/tools
or
/store/items/catalog/tools/{some product}
tried using this rule, but its not working
RewriteRule ^/painting-tools/painting-tools/(.*) /painting-tools/$1 [QSA]
i think the ^ part is not correct since its not the beginning of the URL.
How can i fix it ?
thanks
You can use this back-reference based redirect rule as very first rule in your .htaccess:
RewriteRule ^(.+?/([^/]+))/\2(/.*)?$ $1/$3 [NE,L,R=302]
RewriteRule ^(.+?/tools)/painting-tools(?:/.*)?$ $1/ [L,NC,R=302]
It is capturing repeat value after initial part and grouping it in this sub-pattern ([^/]+). Later in the regex it is using back-reference \2 to make sure same captured value is repeated.
Make sure you have proper RewriteBase defined to either / or /store/ wherever your htaccess is located.
Update: As per discussion below:
RewriteRule ^(.+?/tools)/painting-tools2(/.*)?$ $1$2 [L,NC,R=302]
This will remove /painting-tools2 from URLs.

mod_rewrite .htaccess with %20 translate to -

I have been reading about .htaccess files for a couple of hours now and I think I'm starting to get the idea but I still need some help. I found various answers around SO but still unsure how to do this.
As far as I understand you write a rule for each page extension you want to 'prettify', so if you have something.php , anotherpage.php, thispage.php etc and they are expecting(will receive??) arguments, each needs its own rule. Is this correct?
The site I want to change has urls like this,
maindomain.com/sue.php?r=word1%20word2
and at least one page with two arguments
maindomain.com/kevin.php?r=place%20name&c=person%20name
So what I would like to make is
maindomain.com/sue/word1-word2/
maindomain.com/kevin/place-name/person-name/
Keeping this .php page and making it look like the directory. Most of the tutorials I have read deal with how to remove the .php page to which the argument is passed. But I want to keep it.
the problem I am forseeing is that all of the .php?r=parts of the url are the same ie sue.php?r=, kevin.php?r= and the .htaccess decides which URL to change based on the filename and then omits it. If I want to keep the file name will I have to change the ?r=
so that it is individual? I hope this make sense. So far I have this, but I'm sure it won't work.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1&c=$1
And I think I have to add ([^-]*) this in some part or some way so that it detects the %20 part of the URL, but then how do I convert it to -. Also, how are my $_GET functions going to work??
I hope my question makes sense
You're missing a space somewhere in those rules, but I think you've got the right idea in making 2 separate rules. The harder problem is converting all the - to spaces. Let's start with the conversion to GET variables:
# check that the "sue.php" actually exists:
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/?$ /$1.php?r=$2 [L,QSA]
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$ /$1.php?r=$2&c=$3 [L,QSA]
Those will take a URI that looks like /sue/blah/ and:
Extract the sue part
Check that /document_root/sue.php actually exists
rewrite /sue/blah/ to /sue.php?r=blah
Same thing applies to 2 word URI's
Something like /kevin/foo/bar/:
Extract the kevin part
Check that /document_root/kevin.php actually exists
3 rewrite /kevin/foo/bar/ to /kevin.php?r=foo&c=bar
Now, to get rid of the "-" and change them to spaces:
RewriteCond %{QUERY_STRING} ^(.*)(c|r)=([^&]+)-(.*)$
RewriteRule ^(.*)$ /$1?%1%2=%3\ %4 [L]
This looks a little messy but the condition matches the query string, looks for a c= or r= in the query string, matches against a - in the value of a c= or r=, then rewrites the query string to replace the - with a (note that the space gets encoded as a %20). This will remove all the - instances in the values of the GET parameters c and r and replace them with a space.

.htaccess rewrite rule - custom structure

Is it possible to create a .htaccess rule which will take the middle of a URL structure, but resume the normal REQUEST_URL (Sorry for my terrible explanation)
Take this URL for example
/boats/283/manage/water
Now let's say I'm keeping the hierarchy standardisation as per the URL structure, minus the ID (ID in this case is 287) - so the actual script location is /boats/manage/water(.php)
But obviously I don't have to have a manual rule for each page, as that will get tedious.
eg (What I want to avoid per page).
RewriteRule ^boats/(\d+)/manage/water$ ./boats/manage/water.php?id=$1
RewriteRule ^boats/(\d+)/manage/bacon$ ./boats/manage/bacon.php?id=$1
I have no doubt I could find something relevant in Google, but I just can't quite come up with the proper keywords..
Any help/push in the right direction is much appreciated :)
You can try:
# group out the first path, the ID, then the rest
RewriteCond %{REQUEST_URI} ^/([^/]+)/(\d+)/(.*)$
# pre-check that the destination php file actually exists
RewriteCond %{DOCUMENT_ROOT}/%1/%3.php -f
# rewrite
RewriteRule ^ /%1/%3.php?id=%2 [L]

URL Beautification using .htaccess

in search of a more userfriendly URL, how do i achieve both of the following, elegantly using only .htaccess?
/de/somepage
going to /somepage?ln=de
/zh-CN/somepage#7
going to /somepage?ln=zh-CN#7
summary:
/[language]/[pagefilenameWithoutExtension][optional anchor#][a number from 0-9]
should load (without changing url)
/[pagefilenameWithoutExtension]?ln=[language][optional anchor#][a number from 0-9]
UPDATE, after provided solution:
1. exception /zh-CN/somepage should be reachable as /cn/somepage
2. php generated thumbnails now dont load anymore like:
img src="imgcpu?src=someimage.jpg&w=25&h=25&c=f&f=bw"
RewriteRule ^([a-z][a-z](-[A-Z][A-Z])?)/(.*) /$3?ln=$1 [L]
You don't need to do anything for fragments (eg: #7). They aren't sent to the server. They're handled entirely by the browser.
Update:
If you really want to treat zh-CN as a special case, you could do something like:
RewriteRule ^zh-CN/(.*) /$1?ln=zh-CN [L]
RewriteRule ^cn/(.*) /$1?ln=zh-CN [L]
RewriteRule ^([a-z][a-z])/(.*) /$2?ln=$1 [L]
I would suggest the following -
RewriteEngine on
RewriteRule ^([a-z][a-z])/([a-zA-Z]+) /$2?ln=$1
RewriteRule ^([a-z][a-z])/([a-zA-Z]+#([0-9])+) /$2?ln=$1$3
The first rule takes care of URLs like /de/somepage. The language should be of exactly two characters
length and must contain only a to z characters.
The second rule takes care of URLs like /uk/somepage#7.

Resources