How to add a slash or why it isn't added? - .htaccess

I am currently using Bootstrap to add an accordion function to a jobs page on a new website. Everything works fine, however when I click to view a specific accordion to display the listing, the URL only works like this: website/jobs#developer and not like this: website/jobs/#developer which would look a lot more professional on job boards and maintain what we have on our current site.
I don't want to have to go back to every link distributed and remove the slash. I am attaching what I have done in my htaccess but I am not sure its is affected by it. Please help!
<IfModule mod_rewrite.c>
RewriteEngine On
ErrorDocument 404 /views/errors/404.php
RewriteRule ^(jobs|Jobs|JOBS)$ views/jobs.php [L]
</IfModule>

The rewrite rule will not match a trailing slash
^(jobs|Jobs|JOBS)$
This rewrite rule will not match a url with a trailing slash, it means:
^ # start of match
jobs|Jobs|JOBS # One of these strings
$ # End of match
To have it match urls with a trailing slash, that needs to be in the rewrite rule:
RewriteRule ^(jobs|Jobs|JOBS)/?$
^ A slash
^ 0 or 1 times - i.e. optional
Professional urls?
website/jobs/#developer which would look a lot more professional on job boards
Unrelated to the question, but the presence or absence of a trailing slash doesn't have any bearing on how professional a url looks.

Related

.htaccess remove duplicate part of a URL

some bug in my online catalog SEF URL generation created a situation where some category slug is added to the URL twice. to fix this i would like to use .htaccess to remove the this part of the URL and have the whole URL shift up one level including URL's which also include a product under them.
the duplicate occurrence is of the sub-category painting-tools under the category tools
so the wrong URL looks like this:
/store/items/catalog/tools/painting-tools/painting-tools
or
/store/items/catalog/tools/painting-tools/painting-tools/{some product}
instead of being the correct URL like this:
/store/items/catalog/tools/painting-tools
or
/store/items/catalog/tools/painting-tools/{some product}
and the URLs
/store/items/catalog/tools/painting-tools/
or
/store/items/catalog/tools/painting-tools/{some product}
should actually be like this
/store/items/catalog/tools
or
/store/items/catalog/tools/{some product}
tried using this rule, but its not working
RewriteRule ^/painting-tools/painting-tools/(.*) /painting-tools/$1 [QSA]
i think the ^ part is not correct since its not the beginning of the URL.
How can i fix it ?
thanks
You can use this back-reference based redirect rule as very first rule in your .htaccess:
RewriteRule ^(.+?/([^/]+))/\2(/.*)?$ $1/$3 [NE,L,R=302]
RewriteRule ^(.+?/tools)/painting-tools(?:/.*)?$ $1/ [L,NC,R=302]
It is capturing repeat value after initial part and grouping it in this sub-pattern ([^/]+). Later in the regex it is using back-reference \2 to make sure same captured value is repeated.
Make sure you have proper RewriteBase defined to either / or /store/ wherever your htaccess is located.
Update: As per discussion below:
RewriteRule ^(.+?/tools)/painting-tools2(/.*)?$ $1$2 [L,NC,R=302]
This will remove /painting-tools2 from URLs.

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 file url rewrite keeps going to the wrong page

I am trying to get the follow urls to work
www.urlname.com/team/1-Test
www.urlname.com/team/1-Test/members
RewriteRule ^team/([^-]+)-([^&]+)$ index.php?p=teamprofile&team_name=$2&team_id=$1
RewriteRule ^team/([^-]+)-([^&]+)/members$ index.php?p=teammembers&team_name=$1&team_id=$2
but when i try the link with /members init it goes to the other page?
can someone help me please
Thanks
[^-] and [^&] includes the / so /members is included with that. you could either add / to your negation character groups like [^-/] and [^&/] so it doesn't match / or move the bottom one up and add [L] after it to tell apache this is the [L]ast rule to check if it matches.
The trouble is, your second rule is being satisfied by the first rule. You could simply switch them around and it will work:
RewriteEngine On
RewriteRule ^team/([^-]+)-([^&]+)/members$ index.php?p=teammembers&team_name=$1&team_id=$2
RewriteRule ^team/([^-]+)-([^&]+)$ index.php?p=teamprofile&team_name=$2&team_id=$1
Although, a slight change in the first rule will also address the problem:
RewriteEngine On
RewriteRule ^team/([^-]+)-([^/]+)[/]?$ index.php?p=teamprofile&team_name=$1&team_id=$2 [L]
RewriteRule ^team/([^-]+)-([^/]+)/members[/]?$ index.php?p=teammembers&team_name=$1&team_id=$2 [L]
Note, I changed the match string in the first rule from ([^&]+) to ([^/]+) - that way the forward slash isn't included in the match in cases like mydomain.com/team/1-2/. The [/]? rule at the end is an optional match for that trailing forward slash. I've likewise added one to the end of the members rule as well, now it works like this:
mydomain.com/team/1-2/ - goes to index.php?p=teamprofile&team_name=1&team_id=2
mydomain.com/team/1-2 - goes to index.php?p=teamprofile&team_name=1&team_id=2
mydomain.com/team/1-2/members - goes to index.php?p=teammembers&team_name=1&team_id=2
mydomain.com/team/1-2/members/ - goes to index.php?p=teammembers&team_name=1&team_id=2

Resources