Rewrite %20 for a hyphen width one variable - .htaccess

I am strugling for a few hours with a code now..
I want to change spaces or %20 in a hyphen (-). I tried a lot but nothing will works.
This is my current code:
RewriteEngine On
RewriteBase /sitecms/
RewriteRule ^([a-zA-Z]+)$ page.php?page=$1
sitecms is my root, when $1 = test, I can go to localhost/sitecms/test.
But when $1 = test test, I got localhost/sitecms/test%20test, and I need localhost/sitecms/test-test.
Can someone help me with a solution? I only needs to rewrite, not to redirect

Related

.htaccess work with and without trailing slash

Using .htaccess, how can I get my website to be able to do this. Here is what I have already.
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)/(.*)$ sites/index.php?url=$1&page=$2
From this, I am able to be able to browse to:
http://domain.com/sites/example/
and that works totally fine.
What I want to be able to do is browse to
http://domain.com/sites/example
with no trailing slash, but it always gives back an error with page not found.
So far from Google, all of the results have failed me.
Thank you.
Try changing your regex to:
RewriteRule ^([^/]+)/(.*?)/?$ sites/index.php?url=$1&page=$2
The first (.*) is greedy, and will gobble up everything.
Yeah the sea of info in google can sometimes be a hinderance:
RewriteRule ^(.*)/(.*)/?$ sites/index.php?url=$1&page=$2
Note the /? in the rewriterule before the $, sorry I've read your question backwards...
edited answer:
RewriteRule ^(.*)/?(.*)$ sites/index.php?url=$1&page=$2
The different position of the ? might help as otherwise the greedy match of the first(.*) might glob to the end and try and shove that all into $1, for the specifics you'd be best turning on the apache rewrite log and looking at that if you can.

Using .htaccess to style URL directory style

I have searched this question and looked around but can't seem to get this working in practice. This is my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /poker/(.*)/(.*)/$ /poker/?$1=$2
I am trying to get my page to work like this:
mysite.com/poker/page/home
But this just isn't working, I have used 3 different generators and tried typing it manually from tutorials but it is just returning a 404. Any idea's a greatly appreciated, it could be really obvious..
Thanks
You do not have a trailing slash in your example, yet your rule requires one. You can make the trailing slash optional:
RewriteEngine on
RewriteRule /poker/(.*)/(.*)/?$ /poker/?$1=$2
Note however, that a uri /poker/a/b/c/d/e/f/g/ is also a match here - a/b/c/d/e/f will match the first subpattern and g will match the second one, because (.*) is greedy. Be more specific if you wish to match only content between slashes - e.g. ([^/]*)
Well, there's really nothing wrong with the rules that you have if http://mysite.com/poker/?page=home resolves correctly. The only thing is that if this is in an htaccess file, the leading slash is removed from the URI when it's matched against in a RewriteRule, so you need to remove it from your regular expression (or maky it optional):
RewriteRule ^poker/(.+)/(.+)/?$ /poker/?$1=$2
And maybe make the groupings (.+) instead so that there is at least one character there.

Mod_Rewrite to /subdirectory and /subdirectory/query

I'm having a difficult time getting into using mod_rewrite. I've been at this for about an hour googling stuff but nothing quite seems to work. What I want to do is change
example.com/species.php into example.com/species
and also
example.com/species.php?name=frog into example.com/species/frog.
Using
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^species/(.*)$ /species.php?name=$1
I can get example.com/species.php?name=frog to display as example.com/species/frog, and with
RewriteRule ^species/ /species.php
I can get example.com/species.php to display as example.com/species/, but I can't get both of them to work at the same time.
Also, example.com/species with no trailing slash always comes up as a 404.
I've considered just making a /species/ directory to catch any problems but I'd rather just have a few rules for one species.php file. Any help would gladly be appreciated!
Edit (because I can't answer my own question for 8 more hours):
I seem to have fixed both of my problems. I changed my .htaccess to:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^species/(.*)$ /species.php?name=$1
RewriteRule ^species/?$ /species.php
The second RewriteRule successfully redirects example.com/species to example.com/species.php while leaving the other RewriteRule working at the same time.
However, if I typed in example.com/species/ with a trailing slash, it was being read as example.com/species.php?name= and would throw an error because no name was submitted, so I just added
if(isset($_GET['name']) && empty($_GET['name'])) {header('location: http://example.com/species');}
so that if I used example.com/species/ it would redirect to /species and work as desired.
If you change the * (match zero or more) to a + (match one or more) in your first RewriteRule then you should stop seeing species.php?name= if a trailing slash is used.
This is because the + will require that something appears after the slash, otherwise the rule will not match. Then your second RewriteRule will match because it ends with an optional slash, but will not add the name= query string to the target URL.
You may also want to add the [L] flag (last) after the first rule, because you don't need the second rule to execute if the first rule matches. (Note that this will not stop the RewriteCond and RewriteRule tests being run on the resulting redirect URL, which will have to go through the .htaccess file just like any other request.)
See the Reference Documentation for mod_rewrite in Apache 2.4 (or see the docs for the version of Apache you're actually using).

htaccess is not working

I am trying to write an htaccess rewrite rule. But it is not redirecting,
This is my present rule
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ question.php?qkey=$1
that will show a url like sitename/questionkey and redirect it perfectly.
Now Iam trying to show a url like sitename/questioncatagory/questiontititle
Iam trying to use the following rule, but it is not working
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/^([a-zA-Z0-9]+)/^([a-zA-Z0-9]+)$ question.php?qkey=$1
First, it's probably better for clarity and maintenance to replace your ([a-zA-Z0-9]) with ([\w]+)
Secondly, your new rule doesn't work because of the caret ^ character. In the beginning, it indicates 'match beginning of the line', which surely doesn't apply 3 times total in the regex. Remove the two later ^ carets (and then make use of your additional captured groups somewhere with $1, $2, et c).
Lastly, you probably don't need to match the end of the line with the $ character. This is unfriendly to many URLs, for example ones with a trailing slash.
RewriteEngine On
RewriteRule ^([\w]+)/([\w]+)/([\w]+) question.php?qkey=$1&cat=$2&qtitle=$3

htaccess rewrite, url doesn't work when i add a / at the end

I have got a problem that seems to me something simple but i'm new to htaccess and can't find the solution.
i've got the following line in my htaccess (root map)
RewriteRule ^page1/([a-zA-Z0-9_-]+)/$ page1.php?name=$1
RewriteRule ^page1/([a-zA-Z0-9_-]+)$ page1.php?name=$1
When i enter the the following url it works without a problem
www.myexample.com/page1/variable
the strange thing happens when I add a / at the end. Then the page can't get the GET value out of the URL.
Thanks for your time and help!
Get rid of the ending /$ sign on the first rule
RewriteRule ^page1/([a-zA-Z0-9_-]+) page1.php?name=$1
Or you can continue to capture data
RewriteRule ^page1/([a-zA-Z0-9_-]+)/(.*)$ page1.php?name=$1
Ultimately if you want to keep capturing more data with "/" as a delimiter, I suggest doing
RewriteRule ^page1/(.*)$ page1.php?url=$1
Then use the server side script to determine what to do.

Resources