htaccess - adding prefix to new url - .htaccess

how am I suppose to add a prefix to new url?
RewriteEngine on
RewriteRule ^(.*) http://localhost/redirect/s_$1 [R=301,L]
I want it to redirect to new url with s_ prefix example s_about.html
but it it redirects me to this
http://localhost/redirect/s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_s_index.html

Try with below rule we are skipping urls which have s_ prefix.
RewriteEngine on
RewriteRule ^\b(?!s_)(\w+)$ http://localhost/redirect/s_$1 [R=301,L]

Related

Dynamic url's redirect from old to new url's using .htaccess

I want to redirect below two dynamic url from old url's to new url's using .htaccess and i have done this application using codeigniter.
1) This are the dynamic url's and there are more than thousands of url's in my database.
Old(From) URL
https://www.example.com/area-name/pangothe-263001
New(To) URL
https://www.example.com/pangothe-263001
2) This are the amp version of the above url.
Old(From) URL
https://www.example.com/amp-area-name/pangothe-263001
New(To) URL
https://www.example.com/pangothe-263001/amp
I have tried with below code
RewriteRule ^area-name$ http://www.example.com/area-name [R=301,L]
RewriteRule ^amp-area-name$ http://www.example.com/amp-area-name [R=301,L]
But not redirecting to the new urls. How can do this redirect using .htaccess.
Keep these 2 redirect rules just below RewriteEngine line:
RewriteEngine On
RewriteRule ^area-name/(.+)$ /$1 [R=301,L,NC,NE]
RewriteRule ^amp-area-name/(.+)$ /$1/amp [R=301,L,NC,NE]
# remaining rules go below this
Try This
RewriteRule ^area-name/pangothe-263001/?$ $1/pangothe-263001$2 [R=301,L]
Try with below rules,
Canonical links Redirect
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/area-name/(.*)$
AMP links Redirect
RewriteRule ^ https://www.example.com/%1 [R=301]
RewriteCond %{REQUEST_URI} ^/amp-area-name/(.*)$
RewriteRule ^ https://www.example.com/%1/amp [R=301]

.htaccess mod_rewrite with GET parameters does not redirect

I want to rewrite URLs like:
product_reviews.php?products_id=3
to
haendlerbewertung::_::3.html
In my .htacces I defined:
RewriteRule ^haendlerbewertung::_::([^/]*)\.html$ product_reviews.php?products_id=$1 [L]
This rewrite rule works as it should, but there is no redirection from old to new URL, so each site can be called with 2 different URLs. If I write [L,R=301] the new URL wil be redirected to the old, but that's wrong. The old URL should be redirected to new one. So what's wrong or missing?
You can use the following :
RewriteEngine on
#redirect old url to the new one
RewriteCond %{THE_REQUEST} /product_reviews.php\?products_id=([^\s]+) [NC]
RewriteRule ^.+$ /haendlerbewertung::_::%1.html? [NE,L,R]
# map new url to the old one
RewriteRule ^haendlerbewertung::_::([^/]*)\.html$ /product_reviews.php?products_id=$1 [L]

Htaccess redirect remove part of url if contains certain string

How can I change my url from /images/hold/... to /hold/...
I have this but it doesnt work:
RewriteRule ^/images/hold/(.*) /hold/$1 [R=301,NC,L]
RewriteRule ^/images/hold/(.*) /hold/$1 [R=301,NC,L]
In per-directory .htaccess files the URL-path that is matched by the RewriteRule directive never starts with a slash. So, try the following instead:
RewriteRule ^images/hold/(.*) /hold/$1 [R=301,NC,L]
Or, to cut out some repetition:
RewriteRule ^images/(hold/.*) /$1 [R=301,NC,L]
If you've been experimenting with 301 redirects then you may need to clear your browser cache, as these get cached hard by the browser.

php - Htaccess as fake directories

I am making a mini blog that could make it's url looks like this:
From: http://127.0.0.1/index.php?post=the-story-of-us
To: http://127.0.0.1/view/the-story-of-us
I have tried this but i'm getting 404 not found.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?post=([^&]+)
RewriteRule ^ /view/%2/? [L,R=301]
Your current rule only handles the case: Redirect old url to new url.
(By the way, +1 for using THE_REQUEST to avoid a redirect loop)
You also need to handle the case: Rewrite (internally) new url to old url.
Here is how your htaccess should look like
RewriteEngine On
# Redirect /index.php?post=XXX to /view/XXX
RewriteCond %{THE_REQUEST} \s/index\.php\?post=([^&\s]+)\s [NC]
RewriteRule ^ /view/%1? [L,R=301]
# Internally rewrite back /view/XXX to /index.php?post=XXX
RewriteRule ^view/([^/]+)$ /index.php?post=$1 [L]
I do not udnerstand your RewriteCondition, but the RewriteRule should look like this:
RewriteEngine on
RewriteBase /
RewriteRule ^view/(.*)/? ./index.php?post=$1 [L,R=301]

Endless Redirect Loop by htaccess rules multi language

i'm not good in writing modrewrite access rules and would like to achieve the following scenario:
virtual url /en/shop.php redirects to /shop.php?lang=en
already got that but the actual problems are within the modrewrite environment:
if url is '/' (empty) --> redirect to /en/ (/index.php?lang=en)
if url is '/en' or '/de' --> redirect to /en/ or /de/ (add slash)
if an uri is "defined" like /en/shop.php -> redirect to /shop.php?lang=en
i've tried several rules but the last one results in a endless loop and i can't figure out what's wrong .. :/ please help
here's my .htaccess file:
RewriteEngine On
RewriteBase /
# empty url -> redirect to en/
RewriteRule ^$ en/ [R=301,L]
# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteRule ^(en|de)$ $1/ [R=301,L]
# now all urls have en/ de/ -> parse them
RewriteRule ^(en|de)/(.*)$ $2?lang=$1&%{query_STRING} [R=301,L]
This is the line that is causing the redirect loop:
RewriteRule ^(en|de)/(.*)$ $2?lang=$1&%{query_STRING} [R=301,L]
Because it is redirecting the browser to /?lang=en (for example). Then the first rule:
RewriteRule ^$ en/ [R=301,L]
Redirects it to /en/?lang=en, then the last rule redirects it to /?lang=en, then the first rule, etc.
You probably meant the last rule to be internal, additionally, your first rule needs to check if there's already a query string parameter, "lang":
RewriteEngine On
RewriteBase /
# empty url -> redirect to en/
RewriteCond %{QUERY_STRING} !lang=(en|de)
RewriteRule ^$ en/ [R=301,L]
# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteRule ^(en|de)$ $1/ [R=301,L]
# now all urls have en/ de/ -> parse them
RewriteRule ^(en|de)/(.*)$ $2?lang=$1&%{query_STRING} [L]
# no "R=301" here --------------------------------------^

Resources