How to set the right conditions in htaccess file? - apache

I've tried for ages to fix this problem that I have.
On my website I have rewriten some links with the htaccess file. The htaccess file currently looks like this:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?sideID=$1
RewriteRule ^([a-zA-Z0-9_!-]+)/([a-zA-Z0-9_!-]+)/([a-zA-Z0-9_!-]+)$ index.php?sideID=$1&id=$2&title=$3
RewriteCond %{HTTP_HOST} ^www\.mypage\.no$
RewriteRule ^/?$ "http\:\/\/mypage\.no\/" [R=301,L]
The first rule rewrites from:
http://mypage.com/index.php?sideID=home
To this:
http://mypage.com/home
The first rule rewrites from:
http://mypage.com/index.php?sideID=b&id=12&title=a-great-blog-post
To this:
http://mypage.com/b/12/a-great-blog-post
Soo they do what I want too when it comes to rewriting I guess. BUT, the problem is that if im now navigated to any blog post on the site and then tryies to navigate back to whatever other link lets say http://mypage.com/home it will add http://mypage.com/b/12/home instead. That can of course be fixed easily by just using absolute URL's in the navigation. But when I try to make a XML Sitemap, the bot will index every page for each blog ID like so: http://mypage.com/b/12/home, http://mypage.com/b/13/home, http://mypage.com/b/14/home and so on for each blog ID and pagename.
Is there a way to make sure the RewriteRule's apply seperatly or set a condition for them or something like that?? I am a bit noob in this area.
I beg you, please help me! :)

Related

Redirecting a URL that contains a specific part to another URL using htaccess

I would like to know if there is a way to redirect a URL that looks like this:
https://www.example.com/p=d3d82c7c
to
https://www.example.com/polls/poll?poll=d3d82c7c
using htaccess.
What I want to do is to create a more simple link for the users. Is that possible with htaccess only?
PS: the code at the end may change within each URL.
Thank you very much.
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^.*=(.*) polls/poll?poll=$1 [L]
OR with your shown samples if your URI always starts with p then you could try following, but make sure either previous OR this only one of them should be used at a time.
RewriteEngine ON
RewriteRule ^p.*=(.*) polls/poll?poll=$1 [L]
You can use this method:
Redirect 301 /en/php/project.html http://www.example.org/newpage.html

rid of id number in link address using .htaccess

I have this link that works and leads to the correct article:
Article
however I do not want the link to display the id number I want the link to look like this:
Article
This is what I have in my .htaccess file, any help would be appreciated, thanks:
Options -MultiViews
ReWriteEngine On
RewriteBase /
RewriteRule ^news/(\d+)/([\w-]+)/?$ news.php?id=$1&linkAddress=$2 [NC,L,NE]
Is it poosible to remove the 21 id from the link but still use the id to get the variables from database. thanks.
Could you please try following, written based on samples. Please make sure you clear your browser cache after placing these urls in your .htaccess file. Also make sure this new rule is above your already existing news.php rule(shown in your samples).
RewriteEngine ON
RewriteRule ^(news)/(Hello-World)/?$ $1/21/$2 [NC,L]
##Placing OP's rule to serve urls starting with news with news.php in backend.
RewriteRule ^news/(\d+)/([\w-]+)/?$ news.php?id=$1&linkAddress=$2 [NC,L,NE]

Remove query strings from 301 redirect

I am struggling to create appropriate 301 redirects for a site that was originally built using query strings. The old URL structure looks like this:
http://www.oldsite.com/about/index.cfm?fuseaction=cor_av&artID=5049
I want to redirect the entire subfolder (named 'about') to a new page on the new domain. The new domain's URL looks like this:
http://www.newsite.com/info
So, I set up a redirect that looks like this:
redirectMatch 301 ^/about/ http://www.newsite.com/info
It is redirecting just fine, but it's keeping the original URL string attached, so the new URL ends up looking like this in a browser:
http://www.newsite.com/info/?fuseaction=cor_av&artID=5049
I'm definitely not enough of an Apache/301 expert ot know how to fix this. I just want to strip off everything from the ? on.
Really appreciate any help.
two options:
redirectMatch 301 ^/about/ http://www.newsite.com/info?
or:
RewriteEngine on
RewriteRule ^about/(.*) http://www.newsite.com/info? [L,R=301]
question mark at the end seems to be the critical bit. Second one looks a little cleaner (first leaves a question mark at the end of your URL)
Try to add this code into the .htaccess that specified for oldsite.com:
RewriteCond %{REQUEST_URI} ^/about/index.cfm$
RewriteRule ^(.+) http://www.newsite.com/info/ [R=301,QSA]
Follow up?

Using .htaccess mod_rewrite to pass all URLs in a given directory to a single redirect script

Im trying to use mod_rewrite to redirect any call to /real-estate/* to rewrite.php...i know i can redirect everything to rewrite.php with this:
RewriteRule ^(.*)$ rewrite.php?url=$1 [L]
I would like to have my urls formatted like /real-estate/12345/123-anywhere-st ....where the 123-anywhere-st would be ignored, and have /real-estate/12345 sent to rewrite.php...id like the rewrite rule to only be used on /real-estate...all other areas of the site should function as is...Ive searched all over for a good tutorial or cheat sheet, but none that I can find actually explain how to format the mod_rewrite rules, they just give one or two examples and thats it...can anyone help, as well as maybe provide a link to somewhere I can learn
Thanks!
RewriteRule ^/real-estate/(.*)$ rewrite.php?url=$1 [L]

Rewrite To WordPress Page

I have an existing page of /programs/kids.php that I want to load a category page from WP. I want the .htaccess file in /programs to handle this rewriting for me.
Something along the lines of:
RewriteEngine on
ReWrite kids2.php http://www.mysite.com/blog/cat/kids/
Any help would be awesome.
If your sure mod_rewrite is enabled by apache, the it is simple as
RewriteEngine on
RewriteRule kids.php http://www.mysite.com/blog/cat/kids/
or
RewriteEngine on
RewriteRule ^programs/kids.php$ http://www.mysite.com/blog/cat/kids/
depending on location and strictness (^hhh$ matches the whole string)
The directive is named RewriteRule and not just Rewrite. So try this:
RewriteRule ^programs/kids2\.php$ /blog/cat/kids/
But if you want have requests to /blog/cat/kids/ rewritten internally to /programs/kids2.php (the exact opposite of what you’ve mentioned), try this rule:
RewriteRule ^blog/cat/kids/$ programs/kids.php [L]
You could also do an include for that kids.php in the content of an existing WP page, but you'd need the ExecPHP plugin installed.
Then kids.php would display as styled content inside a WordPress page. Which is sometimes desirable, from a site cohesiveness standpoint.
Something like this:
<?php require_once(ABSPATH. '/programs/kids.php');?>
Not exactly what you asked for, but maybe an alternative approach.