.htaccess redirect url and remove #591 from resulting url - apache

I've a backlink of my website as
http://localhost/babycare/article/detail/93#591
I want to redirect it to a new user-friendly url like
http://localhost/babycare/article/detail/feeding_aversion
The code i've written in my .htaccess file is here
RewriteCond %{THE_REQUEST} /article/detail/93 [NC]
RewriteRule ^/?(babycare)? /article/detail/Feeding_Aversion? [R=301]
But it redirects to
http://localhost/babycare/article/detail/feeding_aversion#591
How could i remove #591 from url.

The problem you've got is that location hashes usually only exist in the browser, and are not meant to be handled by the server.
Its covering in more detail in the following answers;
URL Fragment and 302 redirects
Which one is better pushstate or location.hash?
You could use Javascript to remove the hash's if they're that much of an issue.
location.hash = "";
Should achieve what you're looking for - obviously it'll need to go onto each page.

Related

.htaccess redirect link to same url but adding query string

I'm trying to use pretty affiliate urls based on the username of my website users.
I need to redirect a link like:
https://example.com/anything-here/username
To
https://example.com/anything-here/?ref=user_id
I've found this solution:
RewriteRule ^(.*)username(.*)$ https://example.com/?ref=user_id [L,R=301]
It works, but redirects to the page I set here (in this case, it's the home page). My question is:
How can I redirect it to the page requested at the beginning, but removing the username and adding /?ref=user_id?
You look as if you are close, you just need to make use of the backreferences. Try the following:
RewriteRule ^(.*)/username$ https://example.com/$1/?ref=user_id [R=302,L]
The $1 is a backreference to the first captured group in the RewriteRule pattern.
Also, you don't appear to need the (.*) at the end of the pattern, since username is at the end of the URL (at least in your example).
This is a 302 (temporary) redirect - change it to 301 (permanent) if this is required.
You will need to clear your browser cache before testing.

If specific URL not found - 301 to root

Basically I'm trying to edit my .htaccess file to do the following:
I want to 301 a URL to another URL but ONLY if that URL is not found (404).
Thus the following will not suffice, seeing as it will redirect the URL regardless of whether or not the URL was found.
Redirect 301 /oldpage.html http://www.example.com/newpage.html
Is this possible to do through .htaccess?
And yes, I know this might be an odd request but I have my reasons for needing this.
Try this mod_rewrite rule in your root .htaccess:
RewriteEngine On
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^oldpage\.html http://www.example.com/newpage.html [L,NC,R=302]
You really don't want to 301 a URL to another URL. Permanent redirects cause all sorts of problems which cannot be reverted.
I want to 301...if that URL is not found (404).
Again, that's a really bad idea. Even if your deployment and testing processes are perfect you need to be able to see and respond to the requests coming in.
Use an error document to display a message with a meta redrect after a delay to bounce the user back to the home page.

htaccess page to page redirect and seo friendly urls

i have a problem with a htaccess files and i cannot figure it what is the problem.
The site has url rewriting for seo purposes in place so:
www.website.com/page/seo-friendly-url
is rewritten to
www.website.com/page.php?seo=seo-friendly-url
this is done with the following
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
Now the problem is that i have to redirect some pages that are already indexed by the search engines to their new destination as they are no more available, for example:
www.website.com/page/seo-friendly-url
has to be redirected to
www.website.com/page/another-seo-friendly-url
I have tried something like this but it is not working
Rewriterule ^page/seo-friendly-url$ page/another-seo-friendly-url [R,NC,L]
also this one is not working
Rewriterule ^page/seo-friendly-url$ page.php?seo=another-seo-friendly-url [R,NC,L]
This seems pretty stupid but i can't find the problem :-/
Thank you for your help
Ema
Edit, for anubhava:
Hi,
no i have already set the rewriting for that.
What i'm trying to achieve is redirect an already rewrited link.
Let me explain myself better:
At the moment i have this url that is indexed by Google (or any other search engine) in the form of a beautified url (seo friendly). The url has this form:
www.website.com/page/seo-friendly-url
I have already set a rule in the htaccess so the previous link is rewritten and goes to a php page with a query string that is used to display some content.
The page and the query are in this form:
www.website.com/page.php?seo=seo-friendly-url
So basically i'm using the last part of the first url as a query parameter for the second url.
This is achieved (and works) through the following code here below:
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
So far so good.
Now what i need to achieve is to redirect this url, that has been deleted:
www.website.com/page/seo-friendly-url
to go to a new page
www.website.com/page/another-seo-friendly-url
Of course the same rules applies to this new url (www.website.com/page/another-seo-friendly-url -->is already rewrited to--> www.website.com/page.php?seo=another-seo-friendly-url)
What do i need to do to do the reewriting right?
Thanks
You need this extra rule before your existing rule:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+page\.php\?seo=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
Rewriterule ^page/([\w-]+)$ page.php?seo=$1 [NC,L,QSA]
Just add redirects like this:
RewriteRule page/seo-friendly-url /page/new-url [R=301,L]
Important: this rules have to be above your existing rewrites because of the L flag in your rewrites
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l
Edit
You want to redirect the old URL to avoid duplicate content (rewrite=internal, redirect=HTTP 301)
Maybe you are open for solutions thinking in another direction.
I would try to handle this in the application, no through rewrites. Right now the GET parameter seo is handled in page.php. Isn't it an idea to extend this in that way one product can be identified through multiple seo aliases? If one product has to be taken off a similar one will then own this alias (simply a change of one row in the database).
As I don't know what software you are using this may be not possible.

htaccess permanent redirect and remove string from url

How can I go about getting visitors who enter the site via a mobile url get redirected to the non mobile url on this example;
user visits http://website.com/*mobile/*the-page.html
redirect them to http://website.com/the-page.html
Doing a google search, I was able to come up with the follow htaccess line:
RewriteRule ^/?mobile/(.*)$ /$1 [L,R=301]
though it correctly redirects to the right page, it uses the filename of the dynamic page to do so, example:
http://website.com/index.php?id=the-page
how can I get it to just take out mobile/ from the url?
okay, slow moment... I needed to deleted the original rewrite rules for the mobile site.
after that, it was simple as adding
RewriteRule ^mobile/(.*)$ /$1 [L,R=301]
to htaccess.

.htaccess redirect doesn't hide url

my .htaccess in the root folder includes the following lines :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC]
when I open the address http://example.com/bla.htm, my browser doesn't hide the GET values specified in the .htaccess, it redirects me to ?city=bla. eventhough I'm not using the [R] switch. This has always worked for me before (as I remember, haven't dealt with htaccess in a while). What's wrong here ?
When you redirect to an entire URL, it doesn't do URL rewriting (you can't exactly rewrite URLs on someone else's website).
Assuming both URLs are on the same server, you need to do something like
RewriteRule ^(.*)\.htm$ index.php?city=$1 [NC]
Also, I'd recommend getting into the habit of using the [L] switch whenever you can - it helps avoid bugs when you have a lot of URLs to rewrite.