mod_rewrite issues with URL if page title has dashes - .htaccess

I have a page that is normally like this:
http://www.url.com/folder/content.php?name=this-is-the-page-title&item_id=129
As you can see, the title of the page is included in the URL, separated by dashes.
So, I'd like to convert this to the following with mod_rewrite:
http://www.url.com/this-is-the-page-title-129.html
For this, I use a mod_rewrite rule like:
RewriteRule ^([^-]*)-([^-]*)\.html$ /folder/content.php?name=$1&item_id=$2 [L]
Unfortunately, using that rule, I get a 404 error. I think the problem is because the title is separated by dashes (-) and the separator itself is a dash as well, so it likely can't tell the variables from each other or something like that.
When I change the rule from dash (-) to slash (/) like this it works fine:
RewriteRule ^([^-]*)/([^-]*)\.html$ /folder/content.php?name=$1&item_id=$2 [L]
But then the URL becomes:
http://www.url.com/this-is-the-page-title/129.html
...which I don't want as I'd have to rewrite the entire structure of the page.
Is there any way to get it working as
http://www.url.com/this-is-the-page-title-129.html
even with the page title being separated by dashes?
Thank you :)

The problem is, you specify ([^-]*) which means that the matching sub-pattern should not contain any dashes... but your page title might contain them.
So, instead, lets let the first part by anything but slash:
RewriteRule ^([^/]*)-([0-9]+)\.html$ /folder/content.php?name=$1&item_id=$2 [L]
This way, everything before the last dash will go into the first sub-pattern, and digits after the last dash - into the second one.

Related

htaccess, detect if starting with specific character

Currently I have my htaccess configured so that when I type anything after the domain, it is treated as a get
RewriteRule ^(.*)$ /index.php?id=$1
so
www.example.com/test
will redirect to
www.example.com/index.php?id=test
now what I would like is the page to detect if after the first / there is the # character and do something diffirent
for example,
www.example.com/#test
goes to
www.example.com/index.php?abc=test
whilst still retaining the first rule, can this be done?
And as a bonus, if you know how to use the # symbol instead of the #, please do let me know, I tried putting NE flag in my rule but I had no luck.

Rewrite rule to remove all non alphanumeric symbols from a URI

I could not find the exactly the same question on SO. I hope someone can help me out with this.
Say, user entered http://www.example.com/abc#!def, and what I want to do is remove all symbols in the ${REQUEST_URI} portion, then do a redirect to http://www.example.com/abcdef. The problem is that these symbols can occur anywhere in the string, e.g. #ab!cdeg and abcdef#! should both redirect to abcdef.
If I'm correct, there is no string replace function for mod_rewrite, so this seems impossible to do, but am I correct?
You can capture specific parts of an URL with regular expressions in a RewriteCond
or RewriteRule, but not remove arbitrary characters.
Furthermore, you will never see the hash character '#' and everything after it in a URL, because it is used by the client to navigate to a specific part of the document.
Update using the next flag:
RewriteRule (.*)[^a-zA-Z](.*) $1$2 [N]
This rule removes all characters, which are not ^ alphabetic.

Image Rewriting

This is probably more of a check than a question, but is the following correct:
RewriteRule ^_images/(.*)_(.*)_(.*)$ /_images/$2_$3 [L]
Basically I want to automatically make webpages use /_images/picture-name_ABC123_600.jpg (which doesn't really exist) but actually use the image /_images/ABC123_600.jpg that does exist.
I gave it a check locally in XAMPP, and worked, however I though that I'd better try adding more underscores eg. /_images/picture-name_A_B_C_1_2_3_600.jpg which I was expecting to work, yet the image still appeared - so is this normal behavour?
I'm also not too sure if I can use an underscore between the two dollar signs above...
Any help would be great!
This will not work as the (.*) is greedy and matches as many characters as it can, even underscores. Something like this (not allowing underscore between the separators):
RewriteRule ^_images/([^_]*)_([^_]*)_([^_]*)$ /_images/$2_$3 [L]
or this (ungreedy matching):
RewriteRule ^_images/(.*?)_(.*?)_(.*?)$ /_images/$2_$3 [L]
or a combination of both should work. (Depending of what chars are allowed within each block)

how to rewrite in a htacess file

im into SEO and friendly URL's and im trying to create a rule in my htacess file and i need help...
Basically, i have a list of alphabet letters. If the users selects one letter, the db will show all the lyrics that starts with that letter...
so if i click C, there will be a list of lyrics and the the first is 'Car and blues'
So, from this
htpp://www.website.com/lyrics.php?letter=C
i want to do this:
http://www.website.com/lyrics/C/
so far, this is what i have
RewriteRule ^lyrics/$ /lyrics.php?letter=$1 [L]
the rule should be smart enough to pick everything that comes after 'lyrics', in between the 2 slashes, and not what comes after...
Thanks
the rule should be smart enough to pick everything that comes after 'lyrics', in between the 2 slashes, and not what comes after...
Your rule as it stands is looking for exactly lyrics/ with no possibility of anything before or after it (as defined by the ^ and $).
Assuming you're using letters A-Z in only capitals, you can use this:
RewriteRule ^lyrics/([A-Z])/?$ /lyrics.php?letter=$1 [L]
This will look for a single capital letter after the lyrics/ and send that value to the rewrite URL and also match both cases of having a trailing / or not.
the rule should be smart enough to pick everything that comes after
'lyrics', in between the 2 slashes, and not what comes after...
I'd suggest you look into using regular expressions to format your url. See this link

htaccess redirect/rewrite help

I want to redirect domain.com/1/file.php to domain.com/2/file.php and domain.com/1/pic.jpg to domain.com/2/pic.jpg etc. I want to redirect the subdirectory while leaving to end file in tact. I'm finding it hard to look for a tutorial as I can't describe my problem easily in google. Could someone show me how to do this or direct me to a tutorial about it.
Thanks
RewriteEngine On
RewriteRule ^([0-9])/([a-zA-Z]+).php$ 2/$2.php
Using mod_rewrite...You can do the same thing for your images as well. Essentially the regex expression will collect the value for a backreference. so $1 and $2 since i put 2 regex expressions in there.
Of course, if you need a different match instead of domain.com/1/... you will need to have a regex pattern that matches (but you can just google the appropriate regex expressions you may need but i took your example literally)
Try following for redirect. Replace 1 and 2 with respective subdirectories.
RewriteEngine On
RewriteRule ^1/(.*\.)(jpg|php) /2/$1$2 [L,NC,R=302]

Resources