htaccess URL rewrite page.php?page=string containing a hyphen - apache

I need a solution for my redirect problem. In .htaccess file I have this code:
RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9]+) category.php?page=$1 [NC,L,QSA]
And this is working for a query like
category.php?page=pinturas and redirect to category/pinturas.
But, if the string have - like this:
category.php?page=aquoso-madeira it redirects to category/aquoso
Any help?

category.php?page=aquoso-madeira It redirect to category/aquoso
Because your RewriteRule pattern does not include the hyphen (-), so it matches everything up to, but excluding, the first hyphen.
Include the hyphen in the character class:
RewriteRule ^category/([a-zA-Z0-9-]+) category.php?page=$1 [NC,L,QSA]
Note that the hyphen (-) must go at the start or end of the character class, since this is a special character and has alternative meaning when used elsewhere.
And this working for query like category.php?page=pinturas and redirect to category/pinturas
Note that this rewrite does the complete opposite of what you are describing.
This rule takes a request like category/pinturas and internally rewrites it to category.php?page=pinturas. (There is no external redirect here - which might be in another part of your code?)

Related

301 Redirects Rewrite Rule to replace underscore with hyphen and remove subdirectory

I need help creating a rewrite rule/301 redirect for the following link structures in the .httaccess folder.
Source URL: www.example.com/sub_directory/product_name_1.html
Destination URL: www.example.com/prodcut-name-1.html
The requirements for the redirect then are as follows:
Remove /sub_directory/
Change all underscores '_' to hyphens '-'
Unfortunately my regex isn't very good. I've tried searching around and but the solutions from other post with similar issues where not working for me (such as here)
Any help on a solution for this would be much appreciated. Also if you could please explain the why/how of it. I'd like to be able to better understand this.
Answer from #Walf is close but requires some changes e.g. regex anchors and DPI flag.
You can use these rules on top of your site root .htaccess:
RewriteEngine On
# remove /sub_directory/ when there is no _ left
RewriteRule ^sub_directory/([^_]+)$ /$1 [R=301,NC,NE,L]
# use recursion based rule to replace _ by -
RewriteRule ^(sub_directory/[^_]*)_+(.*)$ $1-$2 [NC,N,DPI]
# rest of your rules go here
Something like this
RewriteEngine on
RewriteBase /
RewriteRule ^(sub_directory/[^_]*)_+(.*) $1-$2 [DPI,N]
RewriteRule ^sub_directory(?:$|/(.*)) http://%{HTTP_HOST}/$1 [R=301,L]
It first loops through URLs that are in that subdirectory, to replace consecutive underscores with a single hyphen. It's done first so it doesn't interfere with other URLs that may contain an underscore. It then externally redirects (the cleaned) requests for that subdirectory to the root. The ugly grouping makes sure it only applies to exactly that folder.

If in htaccess for 301 Redirect special characters anywhere in URL to another characters?

Rule: Redirecting all URLs that contains %252C+ to ,- and redirecting + to - only if the + comes with %252C+.
e.g1: http://www.example.com/FortWorth%252C+TX/
to: http://www.example.com/FortWorth,-TX/
e.g2: http://www.example.com/Fort+Worth%252C+TX/
to: http://www.example.com/Fort-Worth,-TX/
e.g3: http://www.example.com/Fort+Worth/
to: http://www.example.com/Fort+Worth/
(Should not redirect this one)
Note: Please remember your code should not be only for the above URL but for all URLs with the above rule.
Thanks Sumurai8 , for your solution but it needs to be modified like This code :
RewriteEngine On
RewriteRule ^([^+]+)\+(.+)%2C+\+(.*)$ /$1-$2,-$3 [R,L]
RewriteRule ^([^+]+)%2C+\+(.*)$ /$1,-$2 [R,L]
The first argument of RewriteRule is matched against the %-decoded url, so it should be matched against http://www.example.com/FortWorth%2C+TX/ for example instead of http://www.example.com/FortWorth%252C+TX/.
Assuming the strange substring only occurs once, and the "+" only appears once before that, you can do it with the following two rules. If multiple +'s can occur, things start to turn ugly quickly, because you'll need to either add a lot more rules, or start handling it recursively.
RewriteRule ^([^+]+)\+(.+)%2C\+(.*)$ $1-$2,-$3 [R,L]
RewriteRule ^([^+]+)%2C\+(.*)$ $1,-$2 [R,L]
I am not sure if mod_rewrite will escape the , character as a "special character". It is a valid character to use in an url. If it gives you problems, you could use the [NE] flag to prevent escaping such characters.

.htaccess regex url redirection

I'm looking for a regular expression for a .htaccess file where I can redirect one URL to another.
URL1:
/dir/an-article-title-222
URL2:
/dir/222/an-article-title
I would like URL1 to 301 redirect to URL2.
In highlevel language, I think I want to tell .htaccess to:
Take the digits after the last dash (-)
Put those digits before the article name with a trailing slash, i.e. 222/
Remove the last dash and digits from the article name
Any thoughts, suggestions, code are welcome!
Try using this rule :
RewriteEngine on
RewriteRule ^dir/([^/]+)-([0-9]+)$ /dir/$2/$1 [L,R=301]
RewriteRule ^/dir/([\w\d-]*)-([\d]+)$/ /dir/$2/$1 [L,R=301]
Try this, keep in mind that an-article-title by this regex allow a-z, 0-9 and -, for more chars edit the the part [\w\d-]

Conditional rewrite in the regex, not with RewriteCond

I wrote a regex for a rewrite rule that includes a negative lookahead to replace the rewriteCond line, because WordPress only accepts two values: pattern -> substitution.
It should find findme.html _here, regardless of where it's requested to be:
mydomain.com/_here/findme.html
e.g.
(Sorry, I can't modify the swf which will request findme.html in the wrong places)
So, given findme.html could be requested to be in, e.g.:
mydomain.com/findme.html
mydomain.com/directory/findme.html
mydomain.com/directory/findme.html?someparam=3
The rewrite should make them all
mydomain.com/_here/findme.html
So, I made a rewrite rule that Wordpress will accept me as follow
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^.*(?!_here)/*findme\.html$ /_here/findme.html [R=301,L]
So it only matches URLs which doesn't contain "_here" in it, to prevent extra rewriting or a loop.
The problem is IT DOES loop.
What did I miss?
It looks to me like you want to move the .* that is before (?!_here) to after it because (?!_here) is a negative lookahead, so it check that the text _here does not come after it. What your regular expression is actually checking is whether your url starts with some character sequence that is not followed by _here, and _here is a character sequence not followed by _here. Then your rule becomes
RewriteRule ^(?!_here).*/*findme\.html$ /_here/findme.html [R=301,L]
Also, it looks like your pattern will exclude paths with subdirectories such as
mydomain.com/directory/subdirectory/findme.html
If you also want to include those, the pattern should be
RewriteRule ^(?!_here)(.*/)*findme\.html$ /_here/findme.html [R=301,L]

Apache URL Rewrite to domain.com/custom_url_name

Using Apache on a Red Hat server, I'm trying to rewrite the URL of a member's store on our website from:
domain.com/store.php?url=12345
to:
domain.com/12345
Using these rules, I can get it to work if I always remember to add an ending slash:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/url/(.*)$ store.php?url=$1
RewriteRule ^(.*)/$ store.php?url=$1
domain.com/12345/ works,
but domain.com/12345 does not work.
Removing the slash in the last line of Rewrite code breaks a lot of stuff. Is there a way to get this to work both with or without that ending slash?
What if you made the slash optional? Furthermore, you probably to to specify something more specific than (.*), because domain.com/a/b/c/d/e will match. Instead, you can use a negated character class to specify everything other than a slash.
RewriteRule ^([^/]*)/?$ store.php?url=$1
Alternately, if you only want to capture numbers, you can use the \d shorthand class (which matches any digit) along with a + which specifies that at least one digit must be present:
RewriteRule ^(\d+)/?$ store.php?url=$1
Your attempt using ^(.*)$ fails because that would match any URL path. Use a more specific pattern than .*, maybe \d+ to allow only one or more digits:
RewriteRule ^(\d+)$ store.php?url=$1