Simple RewriteRule with no variables - php

I want to change a single URL on mysite from sitename.com/test123.php to sitename.com/article-about-stuff. There are no variables to consider, just a one-off URL change. Is this possible with mod_rewrite and RewriteRule? I currently have:
RewriteEngine on
RewriteRule ^article-about-stuff$ /test123.php [L,QSA]
At the moment the user is redirected, but the url is changed to test123.php. I want to keep the url.
I would also like to be able to redirect any traffic to test123.php to /article-about-stuff
How can I simply write out the destination URL and link to it with the prettier URL?

add the following directive to your .htaccess (this will redirect request from test123 to article-about...)
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^test123\.php$ /article-about-stuff [NC,R=301,L]
Note: the first condition assure that no previous redirection is made (to prevent redirection loop)

The answer to your question is "yes, it's possible," but I'm not sure why you need mod_rewrite for such a simple redirect.
RedirectMatch ^test123\.php$ /test123.php
See When Not To Use Rewrite.

Related

How do I configure Apache (.htaccess?) to use mod_rewrite to pass virtual path to PHP file?

I have a script located at http://www.foo.bar/script and would like to use virtual path names to send data to that script. For example, http://www.foo.bar/script/this/is/a/path would pass "this/is/a/path" to /script. I would like to do this without changing the URL the user sees.
I've already gotten this to work with Apache mod_rewrite using something similar to what was suggested here htaccess mod_rewrite multiple paths to query string. What I have not been able to do is to pass the path to the script without changing the URL the users sees. So, a user that visits http://www.foo.bar/script/stackoverflow/rocks, the script residing at /script would receive /stackoverflow/rocks as a query string or URI but the URL would not change. I know this is not uncommon, and perhaps I'm using the wrong terminology when searching for an answer. Thank you for considering.
One idea someone suggested:
RewriteEngine On
RewriteBase /
RewriteRule "script/(.*)" "https://www.foo.bar/script/?data=$1" [R,L]
However, the above changes the URL in the browser. I don't want to expose "?" on the query string.
I don't really have the exact code you need but I have something I use that might just help you. The example above using rewrite_module will rewrite any URL with .php and will also accept the page name without the .php and redirect to a pageName.php witch is close to what you want, the name the users write, redirecting to the script you want. Additionaly it also offer some protection against cross scripting at the end of the URL.
<IfModule rewrite_module>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{QUERY_STRING} ^(.*?)(?:%3C|%3E|<|>)(.*)$
RewriteRule ^((.*)((?!([.]*p*h*p))\w+)) /$1?%1%2 [L,R=302,NE]
</IfModule>

.htaccess 301 redirect conflicting with rewriterule

I am trying to redirect the URL but its conflicting with the rewrite-rule, I am using this redirection Rule
Redirect 301 /antique-vintage-rugs/170-antique-oriental-rugs-carpets https://example.com/antique-vintage-rugs/170-antique-rugs
it took me to this link
https://example.com/antique-vintage-rugs/170-antique-rugs?action=clear&template=170-antique-oriental-rugs-carpets instead of this
https://example.com/antique-vintage-rugs/170-antique-rugs
My Rewrite URL is this
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^antique-vintage-rugs/(.+) results.php?action=clear&template=$1 [NC]
Is there any way I can get the desired output redirect link?
You need to only be using mod_rewrite here instead of mixing it with mod_alias (where the Redirect directive belongs). When you mix the two, both end up processing the same request.
So instead of Redirect, use:
RewriteRule ^antique-vintage-rugs/170-antique-oriental-rugs-carpets https://example.com/antique-vintage-rugs/170-antique-rugs [L,R=301]
but make sure it's before your other rules.
I had a somewhat similar problem. I was trying to add redirects from cpanel while I already had some rewrite rules written in my .htaccess file. The error I got was "No maching tag for "
What I ultimately did was that kept a copy of my existing rules and cleaned the .htaccess. Then went and added all the redirects that I needed from cpanel, and then at the end put back my own rewrite rules in the end of the file. That worked for me

.htaccess RewriteEngine converting links without changing links

I'm on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.

.htaccess. redirecting urls that has a "go" tag

I'm having a pretty hard time on .htaccess URL rewrites in PHP. I want to put a specific condition to it but it has not worked out yet.
I WANT TO WRITE A CONDITION TO REDIRECT EVERYTHING THAT HAS A "GO" IN ITS URL
RewriteCond %{QUERY_STRING} ^.*&go=([^?&]*)\??([^&]*)&*.*$
Redirect /alice1.html http://www.google.com'
the failed condition that i tried
FOR AN EXAMPLE IF SOMEBODY TYPES
'localhost/alice1.html' IT SHOULD NOT BE REDIRECTED TO google.com
BUT IF IT IS
'localhost//alice1.html?go=www.something_something.com'
THEN IT SHOULD BE REDIRECTED.
You can start from this base rule.
RewriteCond %{QUERY_STRING} \bgo=.+
RewriteRule . http://google.com? [L]
Then refine the checking of the go variable.
edit: note that this is just a template rule matching your input. On a production server it would be better to either set it as an external redirection (using [R=301, L] instead of just [L], assuming you want it permanent) or switch to an appropriate error page (403, 404...)

Redirect a shortened .co url to a full-lengthed .com as Twitter.com has done with t.co (php/apache)

I'd like to make shortened links for my site to be used in Tweets. I'm interested in a t.co-like URLs but confused on how to implement the redirect.
Here's how a link on my site typically looks:
https://mysite.com/item/this-is-a-book-on-toasters
Here's how I'd like the shortened link to look which would redirect to the above link
https://ms.co/Im8y2x
Based on googling how to do this, it looks I need to do a 301 redirect.
I'm using PHP, specifically Codeigniter and I guess there is 2 components: the PHP script and .htaccess.
Here's my .htaccess:
RewriteEngine on
RewriteRule ^/?$ https://mysite.com [L]
RewriteRule ^(.*)$ https://ms.co/$1 [R=301,NC]
The PHP I think I need is in here.
Unfortunately, I can't interpret the answers on this link to make a useful script. Might someone help with this? Also, does my .htaccess look right?
This should be the .htaccess code on your shortlinking website (ms.co):
RewriteEngine On
RewriteRule ^(.*)$ https://mysite.com/in.php?id=$1 [R=301,L]
The in.php should contain the script that decodes the $_GET['id'] (via the short hash decoding methods supplied in the link you supplied), matches it against an ID into your database, and retrieves page that it should redirect to.
By the way, the reason I didn't add a NC part in the code is because upper/lowercase (often) can yield different results when using decoding methods.
I've anaswered my own question. Here's what they .htaccess directive should look like given the question above:
RewriteCond %{HTTP_HOST} ^o-a\.co$ [NC] // the rewrite rule
RewriteCond %{HTTPS} =on //to enable HTTPS
RewriteRule ^(.*)$ https://mysite.com/page-to-handle-hash/$1 [L] //where in your application you want to send the 6-digit hash

Categories