Text in URL after slash as parameter - php

I want to create htaccess rule to turn this url:
www.foo.com/?val=hello
to:
www.foo.com/hello
I created new rule but It is not working.
RewriteRule ^([a-zA-Z0-9]+|)/?$ index.php?s=$1 [QSA]
Any ideas how to do that? Thanks!

I did this on my Apache:
Setup index.phtml. It is define as DirectoryIndex in httpd.conf.
Setup hello.phtml
In my httpd.conf, in the appropriate VirtualHost, I added this:
RewriteEngine on
RewriteCond %{QUERY_STRING} "val=(.+)"
RewriteRule "(.*)" "%1?" [L,R=302]
If I ask for http://localhost/?var=hello.phtml, I get redirected to http://localhost/hello.phtml.
Details:
RewriteEngine on: turn on the rewriting engine. Remember to load the rewrite module (LoadModule rewrite_module modules/mod_rewrite.so)
RewriteCond: variable %{QUERY_STRING} contains everything Apache received after ?. I match this to val=(.+). . matches anything, + means "one or more of the preceding". The preceding here is ., so any combination of characters is valid. It is put in parenthesis since I will need that matched group later.
RewriteRule: "(.*)" matches anything after http://localhost/. It is replaced by %1, which here contains what was enclosed in parenthesis in the RewriteCond above. Then I added ? to ensure the query string is not added.
Note about the ?. By default, if you rewrite from .../somepage.html?var1=value1 to .../somepage2.html, the rewrite engine will automatically add ?var1=value1 at the end of it. To rewrite without keeping the query string, add a single ? at the end.
L: in case you have other rules later, this indicates that it is the last rule to consider if it matches.
R=302. Here I return a 302, temporarily moved. if you want the redirect to be permanent, put R=301.

Related

Custom wordpress HTTP redirects to HTTPS breaks website

I have added extra rules in my .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule !^/articles/(.*)$ https://%{HTTP_HOST}/index.php?name=$1 [L,R=301]
RewriteRule ^(articles/)(.*)$ https://%{HTTP_HOST}/index.php?name=$2 [L,R=301]
</IfModule>
I have two conditions which seems to work:
http://example.com/this-is-old-url/ meets the first rewrite rule
http://example.com/articles/this-is-old-url/ meets the second rule
But the problem is that after these rules are added my website is starting to make redirects everywhere. For example, I can't access my domain https://example.com which changes to https://example.com/index.php?name=. All my website links breaks.
What am I doing wrong?
RewriteCond %{HTTPS} off
RewriteRule !^/articles/(.*)$ https://%{HTTP_HOST}/index.php?name=$1 [L,R=301]
RewriteRule ^(articles/)(.*)$ https://%{HTTP_HOST}/index.php?name=$2 [L,R=301]
The first rule matches everything, since the URL-path that is matched by the RewriteRule pattern in an .htaccess context never starts with a slash (you appear to have recognised this in your second rule). You also can't have capturing groups in negated patterns - by definition, a negated pattern doesn't actually match anything.
And the second rule executes unconditionally, for both HTTP and HTTPS, since RewriteCond directives only apply to the first RewriteRule that follows. (But these two rules can be combined into one anyway.)
To externally "redirect" (as per your example) a URL of the form http://example.com/this-is-old-url/ (or http://example.com/articles/this-is-old-url/) to https://example.com/index.php?name=this-is-old-url then you can do the following at the top of your .htaccess file:
RewriteCond %{HTTPS} off
RewriteRule ^(?:articles/)?([\w-]+/)$ https://%{HTTP_HOST}/index.php?name=$1 [R=301,L]
UPDATE: the regex now includes - (hyphens) and trailing slash. Note that the trailing slash is included as part of the capturing group. So, it redirects to /index.php?name=this-is-old-url/ (with the trailing slash).
The subpattern (?:articles/)? is optional and non-capturing. So, this one rule matches both your example URLs.
The <IfModule> wrapper is not required. And neither is the RewriteEngine directive, since you presumably already have that in the WordPress code block that follows.
You don't necessarily need to expose the index.php, you could redirect to /?name=... instead, assuming the DirectoryIndex is set correctly.
You will need to clear your browser cache before testing. (Preferably test first with 302 - temporary - redirects.)
I can't access my domain https://example.com which changes to https://example.com/index.php?name=.
That couldn't have happened with your existing rules, only if you'd requested http://example.com (note HTTP, not HTTPS).

How to change dynamic url into static url in php

I want to change my dynamic URL link into static URL link while using .haccess, its showing error 500, I have so many links with different URL links name.
Options +FollowSymLinks
RewriteEngine on
RewriteRule product/categoryid/(.*)/productid/(.*)/ product.php?categoryid=$1&productid=$2
RewriteRule product/categoryid/(.*)/productid/(.*) product.php?categoryid=$1&productid=$2
From your question, it is assumed you're running from your domain root. As such, place the following in your /.htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^product/categoryid/(\d+)/productid/(\d+)/?$ /product.php?categoryid=$1&productid=$2 [NC,QSA,L]
Changes made:
I've condensed your two rules into one rule (Don't Repeat Yourself), making the ending forward slash (/) optional
Match beginning (^) and end ($) of the expression
(.*) is now (\d+) which matches digits only (assumed to be IDs)
Added No Case (NC), Query String Append (QSA) and Last (L) flags to the rule
Note: You need to ensure that mod_rewrite is indeed enabled. If the 500 Internal Server Error still persists, please check your Apache logs.

.htaccess 301 redirection removing white space [duplicate]

So here's my problem. I took over a site that has has a bunch of pages indexed that have %20 indexed in Google. This is simply because the person decided to just use the tag name as the title and url slug. So, the urls were something like this:
http://www.test.com/tag/bob%20hope
http://www.test.com/tag/bob%20hope%20is%20funny
I have added a new field for the url slug and string replaced all spaces with dashes. While I have no problem linking to these new pages and getting the data, I need to 301 redirect the old URLs to the new URLs, which would be something like:
http://www.test.com/tag/bob-hope
http://www.test.com/tag/bob-hope-is-funny
So, it needs to be able to account for multiple spaces. Any questions? :)
Use these rules in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)[\s%20]+(.*)$ $1-$2 [E=NOSPACE:1]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
This will replace all space characters (\s or %20) to hyphen -
So a URI of /tag/bob%20hope%20is%20funny will become /tag/bob-hope-is-funny with 301
Brief Explanation: If there are more than 1 space in URI then 1st RewriteRule is fired recursively replacing each space character with hyphen - until there is no space left. This rule will only rewrite internally.
Once no space is left 2nd RewriteRule is fired which just uses a 301 redirect to the converted URI.
Building on #anhubhava's answer, it's close, but that will also match %,2 or 0 in the URL, and it can cause a loop on apache 2.2 if you don't use the DPI parameter. The full script should look like this:
Options FollowSymlinks MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [N,E=NOSPACE:1,DPI]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
I've also added the N (Next) parameter as this then forces the rules to be re-evaluated from the start straight after this rule if it matches. If this isn't there, you can get problems if you're using apache as a reverse proxy as it's unlikely that it'll get to the end of the rewrites before something else happens.

Rewrite Url (URL Routing) in .htaccess

My directories are like:
http://www.mydomain.com/macbook-computer
http://www.mydomain.com/sony-computer
http://www.mydomain.com/lenovo-computer
I want to make that, if a user type computers/macbook-computer like:
http://www.mydomain.com/computers/macbook-computer
I want to display page which is : http://www.mydomain.com/macbook-computer.
My restrictions are:
1. User must type /computers (segment 1)
2. String coming from computers/ must end with "computer". (segment 2)
How can I make this achieve in my .htaccess file?
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/computers/([^-]+)-computer/? [NC]
RewriteRule .* %1-computer? [R=301,L]
Redirects permanently
http://www.mydomain.com/computers/anyname-computer with or without trailing slash.
To:
http://www.mydomain.com/anyname-computer
Strings computers and computer are assumed to be fixed, while anything is assumed to be variable.
The incoming URL structure has to be kept for the rule-set to work: First folder /computers followed by /anyname-computer.
For silent mapping, remove R=301 from [R=301,L]
You need a rewrite rule in your .htaccess file, and a little regular expression magic. Something like this should do the trick
RewriteRule ^computers/macbook-computer$ http://www.mydomain.com/macbook-computer
Here's a nice online tool for checking rewrite rules
http://htaccess.madewithlove.be/

PHP all GET parameters with mod_rewrite

I am designing my application. And I should make the next things. All GET parameters (?var=value) with help of mod_rewrite should be transform to the /var/value. How can I do this? I have only 1 .php file (index.php), because I am usign the FrontController pattern. Can you help me with this mod_rewrite rules?Sorry for my english. Thank you in advance.
I do something like this on sites that use 'seo-friendly' URLs.
In .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]
Then on index.php:
if ($_SERVER['REQUEST_URI']=="/home") {
include ("home.php");
}
The .htaccess rule tells it to load index.php if the file or directory asked for was not found. Then you just parse the request URI to decide what index.php should do.
The following code in your .htaccess will rewrite your URL from eg. /api?other=parameters&added=true to /?api=true&other=parameters&added=true
RewriteRule ^api/ /index.php?api=true&%{QUERY_STRING} [L]
.htaccess
RewriteEngine On
# generic: ?var=value
# you can retrieve /something by looking at $_GET['something']
RewriteRule ^(.+)$ /?var=$1
# but depending on your current links, you might
# need to map everything out. Examples:
# /users/1
# to: ?p=users&userId=1
RewriteRule ^users/([0-9]+)$ /?p=users&userId=$1
# /articles/123/asc
# to: ?p=articles&show=123&sort=asc
RewriteRule ^articles/([0-9]+)/(asc|desc)$ /?p=articles&show=$1&sort=$2
# you can add /? at the end to make a trailing slash work as well:
# /something or /something/
# to: ?var=something
RewriteRule ^(.+)/?$ /?var=$1
The first part is the URL that is received. The second part the rewritten URL which you can read out using $_GET. Everything between ( and ) is seen as a variable. The first will be $1, the second $2. That way you can determine exactly where the variables should go in the rewritten URL, and thereby know how to retrieve them.
You can keep it very general and allow "everything" by using (.+). This simply means: one or more (the +) of any character (the .). Or be more specific and e.g. only allow digits: [0-9]+ (one or more characters in the range 0 through 9). You can find a lot more information on regular expressions on http://www.regular-expressions.info/. And this is a good site to test them: http://gskinner.com/RegExr/.
AFAIK mod_rewrite doesn't deal with parameters after the question mark — regexp end-of-line for rewrite rules matches the end of path before the '?'. So, you're pretty much limited to passing the parameters through, or dropping them altogether upon rewriting.

Categories