Remove /amp from URL with .htaccess - apache

I need to use .htaccess to remove /amp from a certain URL structure. I have incoming links that look like this:
https://domain/store/amp and https://domain/store/product/amp
In both cases, the /amp on the end results in 404 errors. I just need to get rid of it, because fixing the problem at the source is not currently an option. I intend to put the .htaccess in the /store/ directory path.
I have looked around for similar examples and tried to modify them, but rewrites make my head spin in short order. Thanks for the help.

You can use this Redirect
RedirectMatch 301 ^/(.+)amp$ /$1

Related

Trying to redirect anything after the full URL without /68 for example

I need to remove anything on the end of URLs like the below examples without the /68 or any number ID
https://www.website.com/forum/making-coffee/68 <-- I would like to remove the / and anything after so it looks like this below.
https://www.website.com/forum/making-coffee
I've searched but can't get the redirect to work correctly.
Any idea?
You can use this rule in your root .htaccess
RedirectMatch 301 ^/(.+?)/[0-9]+$ /$1

Apache - mod_rewrite how to prefer files instead of directories (if both have same name)?

I have very similar problem like this one: Apache mod_rewrite - prefer files over directories with pretty URLs
However it is not same, and solutions mentioned in above link doesn't work for me.
My directory structure looks like this:
/pages/articles/january.php
/pages/articles.php
/pages/home.php
/articles/
/index.php
Now, I am including in index.php pages (depending on url).
For example, when user types address www.domain.com (or www.domain.com/home), index.php will include /pages/home.php
But if I enter this URL: www.domain.com/articles it will make link something like this: www.domain.com/articles/?page[]=articles (in other words index.php won't include /pages/articles.php file)
On the other hand, this works perfectly: www.domain.com/articles/january
This is my htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9+]*)/([a-zA-Z0-9+]*)$ %{DOCUMENT_ROOT}/index.php?page[]=$1&page[]=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9+]*)$ %{DOCUMENT_ROOT}/index.php?page[]=$1 [QSA,L]
I use array for page because I can have subpages (and subpages work fine!).
EDIT: I have found this however it doesn't solves my problem :(
Can someone tell me why it does this and how to fix it?
Or, how can I give priority to files instead directories?
EDIT 2: I solved it by removing "/articles/" directory, however I am still interested how to make it work through htaccess file rules.
Per the comments above, your original issue was worked around by removing the articles directory, but you still wanted to be able to deal with directories in that sort of situation.
You'd probably want to split into two sets of rules. Have an earlier rule that uses the -d flag in a RewriteCond to catch directories so that it could treat them differently as needed. Alternately, ignore directories in the first rule by negating the flag (!-d) then catch them in the later rule.

htaccess rewrite rules are not working with urls that end with .cfm

I'm working on fixing all my URL's to be shorter with 301 redirects. I have fix almost all of them, however there is a url that is ending with .cfm that will not rewrite.
FROM: http://www.mydomain.com/index.cfm/catlink/17/pagelink/7/sublink/34/art/41/rec/1/page.cfm
TO: http://www.mydomain.com/story/resources/health/page/168/page.html
If I change /page.cfm to /page.html then the rewrite will work.
Here is the rewrite rule that works for my other urls
RewriteRule ^index.cfm/catlink/([a-zA-Z0-9/-]+)([/])pagelink/([a-zA-Z0-9/-]+)([/])sublink/([a-zA-Z0-9/-]+)([/])art/([a-zA-Z0-9/-]+)(.*)$
http://localhost/index.cfm?page=moved&cat=$3&subcat=$5&article=$7&story=$8 [R=301]
Why does it work when the URL ends with .html but not when it ends with .cfm? What am I doing wrong?
This is current link and will not work:
http://www.mydomain.com/index.cfm/catlink/17/pagelink/7/sublink/34/art/41/rec/1/page.cfm
If I manually change the end of it to .html, I can get it to work:
http://www.mydomain.com/index.cfm/catlink/17/pagelink/7/sublink/34/art/41/rec/1/page.html
The issue is that Apache httpd is passing it off to Tomcat before Apache looks at the .htaccess. To test this, move your rewrite rules into your vhost. If they work, then that's what the problem was.
First off, change your the first part of your RewriteRule to be the following, more concise expression:
^index.cfm/catlink/(\d+)/pagelink/(\d+)/sublink/(\d+)/art/(\d+)/(.*)$
I believe that alone might resolve the issue. However, if it does not, and you don't care about the rest of the URL, try the following:
^index.cfm/catlink/(\d+)/pagelink/(\d+)/sublink/(\d+)/art/(\d+)/
Note: this removes the anchor ($) and therefore allows the URL to be open ended.

How can I redirect people accessing my files as directories?

I have the following situation:
On my webserver I have an instance of websvn running, where specific repositories and revisions can be accessed by a URL like
http://www.myhost.com/listing.php?repname=repository1&path=%2Ftrunk%2Fbackend
Somehow, out there in the wild, a wrong URL is being used to access this
http://www.myhost.com/listing.php/?repname=repository1&path=%2Ftrunk%2Fbackend
(Notice the slash after listing.php)
Now, although the URL works and websvn still shows the webpage, images and stylesheets do not get loaded correctly, since they are referenced relative.
I tried to add an .htaccess file to the webroot to redirect people accessing the file as directory to the correct URL.
I have tried multiple variations and ended up with this file:
RewriteEngine on
RewriteRule ^/listing.php/ listing.php [R=301,QSA]
But, since I am writing here, you already guessed it: It doesn't work.
I also tried
RewriteEngine on
RewriteRule ^/listing.php(.*) listing.php$1 [R=301,QSA]
What am I doing wrong?
Perhaps among other things, a RewriteRule within .htaccess that starts with “^/” will never match anything at all. (Examples that include a leading slash are for the global configuration file.) Remove the leading forward slash and see if that helps.
Also, I recommend changing the 301 to a 307 until you get it working. Otherwise, your browser will cache the 301 result, redirecting on subsequent references without consulting your server at all and likely giving you very confusing results.

How do I rewrite www.sitename.com/thing/thing.php?otherthing=something-like-this to www.sitename.com/something-like-this?

How do I rewrite
www.sitename.com/thing/thing.php?otherthing=something-like-this
to
www.sitename.com/something-like-this?
please help me with this as I can't seem to succeed. My host uses apache 2.2. Many thanks for your help!
Update
No I don't need that trailing ? However, I used the Rewrite rule you offered me and it still ain't working. I also added a RewriteEngine On before the rules.
I have Linux hosting, .htaccess and the code is obviously semantically correct, cause otherwise I would get the all so popular 500 internal server error. I placed the .htaccess file in the folder thing and in the root of the site, but it still won't work.
There should be an option to display it in directory format instead of the PHP ? format. If not, you could use the .htaccess mod_rewrite rule to make that display in the /folder/ way.
The way I do it is that I just upload my files and each page name is index.html and then I create folders, and put each index.html in the folder. Like this:
/guidelines/
In that folder is index.html, so instead of it being /guidelines.html it's /guidelines/
Looks better without .html
You need to use mod_rewrite:
RewriteCond %{QUERY_STRING} ^otherthing=(.*)$
RewriteRule ^thing/thing.php$ /%1? [L]
No idea if you meant to have that trailing ? at the end of the rewrite, I don't think that's possible. Note that the ? at the end of the RewriteRule is to get rid of the query string, otherwise, the rewritten URL will still have the ?otherthing=something-like-this at the end.