Mod Rewrite rewrite URL weird behavior, keeps adding query string to displayed URL - .htaccess

I'm a beginner.
What I want
the request URL .../activity/1415/abcdefgh/ to be rewritten to .../activity/gallery.php?k=abcdefgh
but still display the URL to the visitor as .../activity/1415/abcdefgh/.
But when I try, the URL .../activity/1415/abcdefgh/ turned to .../activity/1415/abcdefgh/?k=abcdefgh.
The page displays fine, but I want to hide the query.
Basically what happened
Link says: http://localhost/activity/1516/iolfpqwx
Clicked link
Address bar says: http://localhost/activity/1516/iolfpqwx/?key=iolfpqwx, which I don't want.
My entire .htaccess in the activity/ directory:
RewriteEngine On
RewriteRule ^(\d{4})/(\w{8,})(?:/([\w\s]+)?)?$ gallery.php?key=$2 [L,QSA,NC]
My Specs:
XAMPP on Windows with Apache
What I know
I have not setup any redirect rules on my testing server, so I don't believe I have any [R] flags.
I have a similar .htaccess in an adjacent directory with the code RewriteRule ^settings/([^/]+)/*$ index.php?p=settings&s=$1 [L,QSA,NC] and it didn't show me query strings.
I've Googled my issue, but cannot find a solution. Please help.

Related

Block specific url with ? with htacess

Firstly, I can't get the correct code looking at other post here, not working for me hahahaha.
I would like to block this specific url: /2017/06/wonder-womannuestra-princesa-feminista.html?m=1 (Related to an old blogger url)
Don't know why but different ips, without referrer and user agent, are spamming (not a real visit cause after a redirection to the new one on WordPress they visit all the links in the url) my site always entering by this url and I would like to block ONLY this one.
I've tried to redirect this one with Redirection plugin but I have a redirection to all "?m=(*)" and this one is the one working, not the one related to the specific url.
I just used : RewriteCond %{REQUEST_URI} ^/2017/06/wonder-womannuestra-princesa-feminista.html?m=1 but it doesn't work.
Could you help, I think the problem is that I'm not witting the correct code due to the "?" character. Many thanks.
With your shown samples, attempts considering that you need to block url(mentioned in comments/question), if this is the case then try following. This rule will forbid this specific url from being accessed.
Please these rules at top of your .htaccess file. Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/2017/06/wonder-womannuestra-princesa-feminista\.html\?m=1 [NC]
RewriteRule ^ - [F,L]

My htaccess passthrough rule redirects to the url instead

I'm trying to passthrough (not redirect!) an empty old page to its new location using an htaccess RewriteRule.
I essentially want the user to browse to mysite.com/page-old and to see that url in their browser but be delivered the content from mysite.com/page-new. The user should not be aware that the location changed.
RewriteEngine On
RewriteRule ^page-old/?$ /page-new [PT]
The actual result is that they are redirected to page-new instead.
I found the below on apache.org which seems to validate my code some, but this is giving me a 404 error.
Description:
Assume we have recently renamed the page foo.html to bar.html and now want to provide the old URL for backward compatibility. However, we want that users of the old URL even not recognize that the pages was renamed - that is, we don't want the address to change in their browser
https://httpd.apache.org/docs/trunk/rewrite/remapping.html
RewriteRule "^/foo\.html$" "/bar.html" [PT]
RewriteEngine On
RewriteRule ^example/my-stuff/$ /example/home/ [L,R=301]
check this answer as well
How to redirect a specific page using htaccess

htaccess URL rewrite issue - permalinks

I am having some issues trying to prettify some URL's. Basically I need to make this URL http://domain.com/actualfolder/fakepath/this-is-the-article.php to display content from this page http://domain.com/actualfolder/articlemanager.php.
The articlemanager.php is setup to receive a GET url variable in the way of the "this-is-the-article" and search for it in the database. If found, it will display content specific to that result. This part works.
The part that i can't make it to work is the htaccess rule.
This is my rule but it returns 404 not found or 500 if i don't use the initial slashes.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^fakepath/([^/.]+).php$ articlemanager.php?permalink=$1 [L]
I could really use some help, so any suggestion is welcome.
EDIT: the htaccess file lies in the domain.com/actualfolder/ location

.htaccess Rewrite Based on Existence of Path in URL

Here's the scenario, I have a website that used to be a static HTML site and WordPress blog using a subdomain (http://blog.domain.com).
I recently combined everything into a single WordPress installation. To maintain old links I had to rewrite requests like "http://blog.domain.com/index.php/2010/10/16/post-name" to "http://domain.com/index.php/2010/10/16/post-name". My problem is that when trying to visit just "http://blog.domain.com", I get redirected to "http://domain.com" when I want it to go to "http://domain.com/index.php/blog".
So, if a user requests "http://blog.domain.com" (by itself, with or without slash), I want it to go to "http://domain.com/index.php/blog". If they request an old URL of "http://blog.domain.com/some-link-to-a-post", I want it to redirect to "http://domain.com/some-link-to-a-post". In other words, if it's a URL to an actual post, I just want to strip the "blog" subdomain. If it's the old link to the main blog page, I want to remove the "blog" subdomain and append "/index.php/blog"
http://blog.domain.com/ -> http://domain.com/index.php/blog
http://blog.domain.com/index.php/2010/10/16/post-title -> http://domain.com/index.php/2010/10/16/post-title
Hopefully that's clear. I'm not an htaccess expert, so hopefully someone can help me out here. Thanks in advance!
Using the [L] command at the end of a rewrite will tell htaccess that this is the last rule it should match. If you put a rule to match your first condition at the top and the other rewrite rule you said you had already created after it, you should get your expected result.
Try this:
RewriteRule ^blog.domain.com(/?)$ domain.com/index.php/blog [L]
# Your other rewrite here #
I couldn't get that solution to work. However, I used the following:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/index.php/blog/$1 [R=301,L]
That ends up in a URL like http://domain.com/index.php/blog/index.php/2010/06/04/post-title, but Wordpress is smart enough to fix it.

htaccess rewrite rule 'page numbers' problem

I've added a .htaccess file to my root folder for the purposes of rewriting dynamic URLs into static ones. This seems to have been done successfully but I am having problems with page numbers.
For example, if you were to visit a static page /widgets, the first page of the products is fine....but subsequent pages show up as /products.php?cat=27&pg=2 etc. What I want is for subsequent pages to be in the form of /widgets-pg2 or /widgets?pg=2.
Below is my rewrite rule that I used for the initial category page:-
RewriteRule ^widgets$ products.php?cat=27
If any of you experts can help with this, it would be much appreciated.
Are you expecting the cat to change as well? You'd need to account for that in your URL as well:
e.g. www.site.com/widgets/27/2 could be rewritten as:
RewriteRule ^widgets/([0-9]+)/([0-9]+)$ products.php?cat=$1&pg=$2
If widgets will always be cat 27 then you can change it to:
RewriteRule ^widgets$ products.php?cat=27 [QSA]
which is query string append
Try
RewriteRule ^widgets-pg(.+)$ products.php?cat=27&pg=$1
After that, go here :)
To allow a query string after your rewritten URL use the [QSA] flag:
RewriteRule ^widgets$ products.php?cat=27 [QSA]
A link would than be:
http://example.org/widgets?pg=167&perpage=100&tralala=Hi!
I tried the following but it resulted in a '404 Not Found' error when I go to /widgets:-
RewriteRule ^widgets-pg(.+)$ products.php?cat=27&pg=$1
And I tried the following:-
RewriteRule ^widgets$ products.php?cat=27 [QSA]
This worked correctly but to go to page two of the widgets page, I need to type in the browser:-
/widgets?pg=2
But the actual 'page 2' link still leads to:-
products.php?cat=27&pg=2
So apart from a rewrite rule....maybe I need a separate redirection rule? Or maybe need to change the page number links. But since these are dynamically generated, I'm not sure how to do this.
The following is the PHP code for the page:- http://freetexthost.com/3ubiydspzm

Resources