Remove index from URL after file extension has been removed using htaccess - php

With help on my last question about removing file extensions from the address bar if the page name and file extension were keyed in directly even after using htaccess to remove them, HERE I'm now wanting to remove index from the url. Specifically, if I type in my webpage's URL into a browser like so: http://webpage.com that exact address is what I see in the browser when the page is served up. However, if I click the Home link on my page, which contains a href="index" naturally, what shows in the address bar is http://website.com/index Certainly, I could change the link in my code to a href="http://website.com" to omit the index portion from showing up in the address bar, but that seems like a hack.
I found an SO article HERE that shows a method for removing index.php but I'm a bit lost as to how, or even if the code in that answer (I get particularly stuck when seeing Rewrite Base /
) can be blended in with the code that was given to me HERE to just remove index after the file extensions have already been removed.
My apologies in advance if this isn't a clear explanation, I'm in new territory here. Many thanks in advance!

Doing proper rewriting requires you to build the correct links that you want to see being used by the web (i.e. users and search engines). Don't use URLs in your own links that point to URLs that you consider to be wrong.
The link to the home page likely should be <a href="/">, if you don't like index.
Trying to fix the problem by making the client do two requests, one for index, then receiving a redirect to /, is the hack here. Fix your links - they are under your control for a reason.

Certainly, I could change the link in my code to a href="http://website.com" to omit the index portion from showing up in the address bar, but that seems like a hack.
No, that's what I would do :-)

Related

Strange link behavior in codeigniter

This is one of those problems that make me question my sanity. I'm using php, codeigniter framework, removing index.php from URL with mod rewrite in htaccess.
I'm at this address
http://localhost/health/users/bob/progress/
I click a link that takes me to
http://localhost/health/users/bob/progress/01-04-15
Then I click on a link that takes me back to
http://localhost/health/users/bob/progress/
Now here's the strange thing. I click on the same link that earlier took me to
http://localhost/health/users/bob/progress/01-04-15
But instead, now it's taking me to
http://localhost/health/users/bob/progress/progress/01-04-15
I've gone through the steps a dozen times now. This is definitely the behavior it's giving me. It even happens when I do a full refresh. I actually have to visit a different address, then return to get the link at
http://localhost/health/users/bob/progress/
to work properly again. I'm guessing it's either related to the mod rewrite, or some other rewrite behavior in the codeigniter framework.
I'm pretty sure you are using a relative path for the link that goes something like:
link here
I would suggest that you use CI's base_url instead so that the link becomes absolute:
link here
the base_url is set on your configuration file. Please let us know how it goes!
My guess is that your link looks something like the following, and because of that it get's appended to the current url
href="progress/01-04-15"
Try making your link using absolute path, preferably appending it with base_url or current_url from codeigniter + progress/date.
href="http://localhost/health/users/bob/progress/01-04-15"
That way you will get to the correct link every time, instead of appending it to current url.

MODx - Resource Alias breaking plugin - Using pages with "/" in the URL? ( /resources/page.html )

I currenlty have a MODx website setup, and I am wondering if it's proper to have my links look like this (And by that I mean my resource alias):
myurl.com/help/configuration/basics.html
Putting the slash in the URL like that... is it a bad practice? My main problem is that this breadcrumb plugin seems to no longer work... :(
I want to have this breadcrumb plugin operational again, if someone would mind helping me.
The Error:
When say using the breadcrumb to link to a page that's myurl.com/code/landing.html and the current page is myurl.com/help/configuration/basics.html, it will then redirect you to a link that doesnt exist, that looks like this:
myurl.com/help/configuration/code/landing.html
As you can see, it is appending the latter portion of the new page's url, but it is not properly removing the URL of the current page, as it only removes the current URL up to the '/' slash....
Does anyone know how to fix this? Possibly a way to make it so that instead of doing it's current "remove" and "append" thing, I can change breadcrumbs.class.php to take the value of the links as if I had clicked the link with the "view" button on the MODx manager? I'm not an expert at php, and it is a must for this to work.
Or are there other alternatives to creating the " / " effect in my links, rather than just putting it in the resource alias? (As I want the site to follow a logical, readable, order)
With all things modx, clear the cache, even if you don't think it could be the issue ~ modx is very aggressive about caching stuff.
Though this kinda sounds like maybe your base href tag may be set to <base href="" />, possibly in your templates it may look like:
<base href="[[++site_url]]"></base>
where site_url is set dynamically [it's not a system setting so don't look for it] in your /core/config/config.inc.php as $modx_base_url ... if that value is blank that could be causing the issue.
Also check your rewrite rules in your .htaccess to see what the rewrite base is. [it should be / ]
then refresh your cache... ;)

Change what a link looks like with .htaccess

I have a Joomla based community site and with search engine friendly URLs activated in the backend my profiles are located under mysite.com/community/profile/user/"username"
I need the htaccess file to do nothing unless a URL containing "community/profile/user" is found. If that string is found then it should change the link to mysite.com/"username" but in reality be showing the page mysite.com/community/profile/user/"username"
I think this would be rewrite rule instead of redirect, but I barely know what I'm talking about.
Can someone please tell me what code I must place in my .htaccess file in order to change this? I believe .htacces would be the best way to do what I need, but if you have another idea I'm glad to hear it.
First be sure you understand .htaccess's role.
It is only read when an incomming request is made. So it will not change URLs generated by joomla.
You can however allow urls like mysite.com/eddie to actually pull content from mysite.com/blah/blah/eddie
http://httpd.apache.org/docs/current/rewrite/remapping.html
If you are looking to "train" your users, you can add a step before that to redirect the URL as well. This get's very tricky though as if you're not careful you can get caught in a loop.
user clicks mysite.com/blah/blah/eddie
apache redirects to mysite.com/eddie
(browsers makes second request, user sees URL change)
apache sees mysite.com/eddie and loads the underlying mysite.com/blah/blah/eddie
An easier solution might be to tweak the joomla community code to generate the short urls (mysite/eddie) and use apache to make a call direct to the plugin (mysite/components/communit/index.php?user=eddie

Apache configuration: randomly adds a sub folder to a path

I have a problem.
I ma moving a system from one server to another and I came across a peculiar problem. There are some pages placed in a subfolder like these:
xttp://test.domain.com/admin/oders.php
xttp://test.domain.com/admin/users.php
xttp://test.domain.com/admin/whatever.php
Now, when I move around the pages, via some simple menu with links I get most of the times correct hits. But from time to time I end up on say:
xttp://test.domain.com/admin/admin/oders.php - which obviously causes 404
When I go back to previous page and press the link again it again works all right. Also when I hover over the links they always show proper paths regardless of whether I am going to get 404 or not. All links are dynamically generated by the scripts but they work perfectly on old server and as I say to a naked eye it all looks OK, right until I press the link.
Anyone has an idea where to look for a bug or which tool to use to see what is happening when I press the link? URL mod rewrite? Domain configuration? I am at a loss.
It sounds like the scripts are getting confused between
[xttp://test.domain.com]/admin/file.php
admin/file.php
file.php
Without seeing how the URLs are generated it's impossible to say how this is happening.

Multiple Dynamic SEO/shortened URLs in PHP

I have a PHP system containing user-generated pages, arranged in a complex and non-uniform hierarchy. Pages are created by users, and some pages have sub-pages etc.
I have been asked to add a shortened-url system. So any page, at any point in the hierarchy, can be accessed via domain.com/XXXX where XXXX can be anything - we are not interested in SEO here, the reasoning behind this is its a very social-media driven project, and we would like our users to be able to tweet/other the url of any page they like.
I expect something like; we start on AAAA and head towards ZZZZ as each page is created. Each of these slugs would be stored in the database alongside the actual url eg domain.com/projects.php?p=32
I know mod-rewrite enough to convert domain.com/XXXX into domain.com/index.php?slug=XXXX, but where to go from there leaves me a little stumped. index.php can do the database lookup and header() the user to the actual url, but the slug-url needs to stay in the address bar.
Would using an iframe in index.php be a terrible idea?
I hope thats clear, thanks for reading!
If you used the [R=301] directive at the end of an .htaccess rewrite rule, it will act as a redirect. Meaning if you go to domain.com/XXXX it will show domain.com/index.php?slug=XXXX in the address bar. Is that the behavior you're trying to accomplish?
Also, I wouldn't use a header(), I'd make your index page be the processing page. Either that, or use an include() method instead.
I think using an iframe is a terrible idea, and will lead to a brittle site.
Is there any reason why index.php can't act as a front controller, so instead of redirecting it just shows the page? You should just be able to include the page.
Alternatively, could you not rewrite domain.com/XXXX to domain.com/projects.php?slug=XXXX, and do a slug->p conversion at the top of projects.php? Then the conversion would just need to record slugs and page ids, rather than slugs and full URLs.

Categories