.htaccess rewrite on mediatemple - .htaccess

I'm hosted on mediatemple DV 4.0 server and use the following lines in my htaccess file (rewrite rules for the CMS)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]
it works well but when I'm trying to do a 301 redirect to point the old page to a new address it adds some unnecessary stuff into the url, for instance:
?id=locations-maryland
How do I modify the rewrite rule to keep the same functionality but remove the last part which basically makes the 301 redirect pointless as the CMS couldn't not find any page with this spammy url.
It happens only on mediatemple I heard something about adding/removing an additional question mark somewhere but have not enough knowledge to resolve it by myself.

Add a ? to the end of the redirect target. Example:
Redirect /something/ /something/else/?
or if you're using mod_rewrite:
RewriteRule ^/?something/(.*)$ /something/else/$1? [L,R=301]

Related

Htaccess for redirecting a directory to a file with the same name

After doing a quick lookup on how to manage sites with multiple language support, I find site.com/language/page/ the neatest url layout. (as I don't have the funds for site.language)
I have used htaccess to redirect the base site from site.com to site.com/language/
by using: RedirectMatch ^/$ /language/ where 'language' is language.html
But since I have a directory called /language/ so that all other pages in the given language can be put inside it, the site just shows up as the index of the directory.
How can I accomplish that kind of layout, if I want the main index page to show up in the url as site.com/language/ ?
Current htaccess that worked fine until I added the directory:
## Rewrite Defaults
RewriteEngine On
## Remove file extension + force trailing slash
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule (.*)/$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]
## Redirect to /en-gb
RedirectMatch ^/$ /en-gb/
With the root folder looking like:
/language
stuff.html
morestuff.html
language.html
...
I would really appreciate help as this is currently a nightmare situation. When I try compiling the htaccess with answers to similar questions, everyone has just parts of what I am looking to achieve and thus I break the layout with every modification...
Is it possible to change the back-end filenames and accomplish this layout using only htaccess for front-end url rewriting since the url bar is the only thing that matters?
If I understand correctly, you should be able to solve this by removing the line:
RewriteCond %{REQUEST_FILENAME} !-d
Let me know if that works.

301 Redirect Problems internal page and external page

i need create simple 301 redirect using .htaccess. I have 2 problems :
i want to redirect old url to new url (internal webpage) such as :
from
myweb.com/articles_bla-bla-123.html
to
myweb.com/bestarticles_bla-bla-123.html
So, every URL contain articles_bla-bla-123.html will be redirected to bestarticles_bla-bla-123.html
bla-bla-123 is dynamic text because it is a title of article.
I already use below method but without success :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RedirectMatch 301 ^articles_([a-zA-Z0-9-]+)(\.html) bestarticles_([a-zA-Z0-9_-]+)(\.html) [L]
Any idea how to solve this problem?
Problem no.2 same like no.1 but different domain. For example :
from
http://www.myweb.com/articles_bla-bla-123.html
to
http://www.otherweb.com/bestarticles_bla-bla-123.html
Any idea how to solve this problem?
Thanks so much for anyone who able to answer
I think you need a simple RewriteRule:
RewriteRule ^(articles_.+).html$ best$1
This rule will prepend "best" to every html file request containing "articles_xxxxxxxx"
EDIT:
To change "articles" by another word do it like this:
RewriteRule ^(articles_)(.+\.html)$ readthis_$2

mod_rewrite does not work

I have the following page name
http://example.co.uk/vehicle.php?size=large
and I have written the following rewrite rule so the domain should look as follows
http://example.co.uk/size/large
Here is the rule
RewriteEngine On
RewriteRule ^size/([a-zA-Z0-9]+)/$ vehicle.php?size=$1
I have two problems:
The first is its not working. In fact its not working at all.
The second is if it does work then it will remove the page name which I don't want. I'm struggling to see my mistake in the first place to fix it but I want the domain to look as follows:
http://example.co.uk/vehicle/size/large
If you want to redirect from /vehicle.php URLs to /vehicle/ URLs, then try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*?)=(.*?)$
RewriteRule ^vehicle\.php /vehicle/%1/%2? [R=301,L]
As for displaying contents of nonclean URLs (like /example.php?foo=bar) at clean URLs (like /example/foo/bar), there is a cyclic-redirection issue when trying to use both internal (from a to b) and external (from b to a) redirections at the same time.
If you've decided to switch to clean URLs, then I would recommend you to use /index.php as the only handler for all requests, and use mod_rewrite solely to redirect from old URLs to clean ones. Moreover, I usually perform most of operations with PHP (by parsing $_SERVER['REQUEST_URI'] via PHP), and use mod_rewrite solely to map all requests to index.php file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,QSA]
That's most flexible, straightforward, and portable solution.

Tricky: Remap url but stay on the same url internally

Need your help. Just spend many ours on this htaccess problem and still don't have a clue how to manage this.
I have many http://www.example.com/menu-alias/foo links on my company's website which should get redirected to http://www.example.com/foo.
This alone shouldn't be the hard part but listen up... the tricky part follows.
I don't manage to get the site (Joomla 1.5) working without the 'menu-alias' this means that all http://www.example.com/foo should get internally mapped to http://www.example.com/menu-alias/foo. So that the user still has http://www.example.com/foo in his browser's address bar.
To make it even more complicated i have to 301 redirect the old menu-alias/foo links to /foo.
Can some htaccess guru help me out? Is this even possible?
You can try adding these rules in the htaccess file in your document root (or vhost config):
RewriteEngine On
# externally redirect requests that have "menu-alias"
RewriteCond %{THE_REQUEST} /menu-alias/([^\ \?]+)
RewriteRule ^ /%1 [L,R=301]
# internally rewrite requests back to menu-alias
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/menu-alias/
RewriteRule ^/?(.*)$ /menu-alias/$1 [L]
Couple of potential problems:
Joomla may be looking for the original un-rewritten request in $_SERVER, if so, the rewrite won't work.
The rule to add the /menu-alias/ back into the URI does so blindly rewrites all requests that don't point to an existing resource. This means "virtual" paths that Joomla may handle will get a "menu-alias" appended to the front.

301 Redirecting from old page to new page on Apache not working

A simple 301 redirect is not working in this instance - For example:
Redirect 301 /oldpage http://www.mysite.co.uk/newsubdir/newpage
The site is dynamic and the .htaccess is already renaming pages to search engine friendly URL's from URL's containing a query string.
RewriteRule ^(.*)/(.*)$ index.php?page_name=$1&sub=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)([^/])$ http://www.mysite.co.uk/$1$2/ [R=301,L]
When we are using these 301 redirects like above in the same .htaccess (at the bottom), the pages are redirecting but the query string is getting added to the end of the URL frustratingly and we have not figured out why or how to prevent it.
After 301 redirect, URL is looking like:-
http://www.mysite.co.uk/newsubdir/newpage/?page_name=old-page&sub=
...Causing a 404 error - it's just the query string added on the end of the URL's that is breaking the redirect.
Please can anyone advise on what needs to be done to fix this?
Thanks
Add question mark ? at the end of URL to prevent existing query string to be copied to a new URL:
Redirect 301 /oldpage http://www.mysite.co.uk/newsubdir/newpage?
But since you are already using mod_rewrite, I would recommend utilising it for this task as well (place this rule above your other rewrite rules):
RewriteRule ^oldpage$ http://www.mysite.co.uk/newsubdir/newpage? [R=301,L]
redirect 301 /oldfile.htm http://www.example.com/newfile.htm
Use it. Working fine.

Resources